Skip to content

Instantly share code, notes, and snippets.

@zaynali53
Last active October 20, 2021 13:14
Show Gist options
  • Select an option

  • Save zaynali53/6017436bd647b0ee64d70db83e0b7083 to your computer and use it in GitHub Desktop.

Select an option

Save zaynali53/6017436bd647b0ee64d70db83e0b7083 to your computer and use it in GitHub Desktop.
CPP - Doubly Linked List
#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