-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.cpp
More file actions
84 lines (71 loc) · 1.83 KB
/
DoublyLinkedList.cpp
File metadata and controls
84 lines (71 loc) · 1.83 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
using namespace std;
struct Node{
int data;
struct Node* next;
struct Node* pre;
};
Node* head=NULL;
Node* tail=NULL;
void insertNodeAtBeg(int value){
Node* new_node=new Node;
new_node->data=value;
new_node->pre=NULL;
if(head==NULL){
head=new_node;
tail=new_node;
new_node->next=NULL;
}
else{
head->pre=new_node;
new_node->next=head;
head=new_node;
}
}
void insertNodeAtEnd(int value){
Node* new_node=new Node;
Node* tail=head;
new_node->data=value;
new_node->next=NULL;
while(tail->next!=NULL){
tail=tail->next;
}
tail->next=new_node;
new_node->pre=tail;
}
void insertAfterNode(struct Node* insert_after, int value){
Node* new_node=new Node;
new_node->data=value;
new_node->next=insert_after->next;
insert_after->next->pre=new_node;
insert_after->next=new_node;
new_node->pre=insert_after;
}
void display(){
cout<<"Displaying doubly linked list: "<<endl;
Node* ptr=head;
cout<<"DATA ADDRESS PREVIOUS NEXT"<<endl;
while(ptr!=NULL){
cout<<ptr->data<<" "<<ptr<<" "<<ptr->pre<<" "<<ptr->next<<endl;
ptr=ptr->next;
}
}
void displayBackwards(){
cout<<"Displaying doubly linked list in reverse: "<<endl;
Node* ptr=tail->next;
while(ptr!=NULL){
cout<<ptr->data<<endl;
ptr=ptr->pre;
}
}
int main(){
insertNodeAtBeg(10);
insertNodeAtBeg(20);
insertNodeAtBeg(30);
insertNodeAtBeg(40);
insertNodeAtEnd(50);
insertAfterNode(head,60);
display();
cout<<endl;
displayBackwards();
}