Doubly Link List using Class in C++
Program of Doubly Link List using Class in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//A c++ program to implement linked list #include <bits/stdc++.h> using namespace std; /* A class to create node */ class Node { public: int data; Node *next; Node *prev; }; //A function to insert at the start of the doubly link list void Push_Doubly(Node** head, int newdata) { //create new node Of doubly link list Node* newnode = new Node(); /* put in the data */ newnode->data = newdata; /* As we are inserting value at the beginning,prev is always NULL */ newnode->prev = NULL; /* link new node's next to head */ newnode->next = (*head); /* change prev of head node to newnode */ if((*head) != NULL) (*head)->prev = newnode ; /* changing head node */ (*head) = newnode; } void Dispplay_Doubly(Node *head) { while(head != NULL) { cout << head->data << " "; head = head->next; } } int main() { /* start with an empty list */ Node* head = NULL; Push_Doubly(&head, 9); Push_Doubly(&head, 4); Push_Doubly(&head, 8); cout << "Congratulations: Doubly Linked list is created using classes." << endl; Dispplay_Doubly(head); return 0; } |
Output
Congratulations: Doubly Linked list is created using classes
8Â 4Â 9