Last active
October 20, 2021 13:14
-
-
Save zaynali53/6017436bd647b0ee64d70db83e0b7083 to your computer and use it in GitHub Desktop.
CPP - Doubly Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| struct Node { | |
| int data; | |
| Node* next; | |
| Node* prev; | |
| }; | |
| int main() | |
| { | |
| Node* first = new Node; | |
| Node* second = new Node; | |
| Node* third = new Node; | |
| first->data = 11; | |
| first->next = second; | |
| first->prev = NULL; | |
| second->data = 12; | |
| second->next = third; | |
| second->prev = first; | |
| third->data = 13; | |
| third->next = NULL; | |
| third->prev = second; | |
| Node* pointer = first; | |
| while (pointer != NULL) { | |
| cout << "Current Element: " << pointer->data << endl; | |
| // Next | |
| cout << "Next: "; | |
| if (pointer->next != NULL) | |
| cout << pointer->next->data; | |
| else | |
| cout << "Empty"; | |
| cout << endl; | |
| // ----- | |
| // Previous | |
| cout << "Previous: "; | |
| if (pointer->prev != NULL) | |
| cout << pointer->prev->data; | |
| else | |
| cout << "Empty"; | |
| cout << endl; | |
| // ----- | |
| // Spacer | |
| cout << endl; | |
| // Move next | |
| pointer = pointer->next; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment