37 lines
696 B
C++
37 lines
696 B
C++
|
#include <iostream>
|
|||
|
#include<bits/stdc++.h>
|
|||
|
using namespace std;
|
|||
|
struct Node {
|
|||
|
<EFBFBD> <EFBFBD>int data;
|
|||
|
<EFBFBD> <EFBFBD>struct Node *prev;
|
|||
|
<EFBFBD> <EFBFBD>struct Node *next;
|
|||
|
};
|
|||
|
struct Node* head = NULL;
|
|||
|
void insert(int newdata) {
|
|||
|
<EFBFBD> <EFBFBD>struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
|
|||
|
<EFBFBD> <EFBFBD>newnode->data = newdata;
|
|||
|
<EFBFBD> <EFBFBD>newnode->prev = NULL;
|
|||
|
<EFBFBD> <EFBFBD>newnode->next = head;
|
|||
|
<EFBFBD> <EFBFBD>if(head != NULL)
|
|||
|
<EFBFBD> <EFBFBD>head->prev = newnode ;
|
|||
|
<EFBFBD> <EFBFBD>head = newnode;
|
|||
|
}
|
|||
|
void display() {
|
|||
|
<EFBFBD> <EFBFBD>struct Node* ptr;
|
|||
|
<EFBFBD> <EFBFBD>ptr = head;
|
|||
|
<EFBFBD> <EFBFBD>while(ptr != NULL) {
|
|||
|
<EFBFBD> <EFBFBD> <EFBFBD> cout<< ptr->data <<" ";
|
|||
|
<EFBFBD> <EFBFBD> <EFBFBD> ptr = ptr->next;
|
|||
|
<EFBFBD> <EFBFBD>}
|
|||
|
}
|
|||
|
int main() {
|
|||
|
<EFBFBD> <EFBFBD>insert(3);
|
|||
|
<EFBFBD> <EFBFBD>insert(1);
|
|||
|
<EFBFBD> <EFBFBD>insert(7);
|
|||
|
<EFBFBD> <EFBFBD>insert(2);
|
|||
|
<EFBFBD> <EFBFBD>insert(9);
|
|||
|
<EFBFBD> <EFBFBD>cout<<"The doubly linked list is: ";
|
|||
|
<EFBFBD> <EFBFBD>display();
|
|||
|
<EFBFBD> <EFBFBD>return 0;
|
|||
|
}
|