forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble linked list
146 lines (138 loc) · 3.33 KB
/
double linked list
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<iostream>
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insert_beginning() {
list *addBeg = new list;
cout << "Enter value for the node:" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << "Linked list Created!" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << "Data Inserted at the beginning of the Linked list!" << endl;
}
}
/* Function for create/insert node at the end of Linked list */
void insert_end() {
list *addEnd = new list;
cout << "Enter value for the node:" << endl;
cin >> addEnd->data;
if(first == NULL) {
addEnd->prev = NULL;
addEnd->next = NULL;
first = addEnd;
last = addEnd;
cout << "Linked list Created!" << endl;
}
else {
addEnd->next = NULL;
last->next = addEnd;
addEnd->prev = last;
last = addEnd;
cout << "Data Inserted at the end of the Linked list!" << endl;
}
}
/* Function for Display Linked list */
void display() {
node = first;
cout << "List of data in Linked list in Ascending order!" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->next;
}
node = last;
cout << "List of data in Linked list in Descending order!" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->prev;
}
}
/* Function for delete node from Linked list */
void del() {
int count = 0, number, i;
node = node1 = node2 = first;
for(node = first; node != NULL; node = node->next)
cout << "Enter value for the node:" << endl;
count++;
display();
cout << count << " nodes available here!" << endl;
cout << "Enter the node number which you want to delete:" << endl;
cin >> number;
if(number != 1) {
if(number < count && number > 0) {
for(i = 2; i <= number; i++)
node = node->next;
for(i = 2; i <= number-1; i++)
node1 = node1->next;
for(i = 2; i <= number+1; i++)
node2 = node2->next;
node2->prev = node1;
node1->next = node2;
node->prev = NULL;
node->next = NULL;
node = NULL;
}
else if(number == count) {
node = last;
last = node->prev;
last->next = NULL;
node = NULL;
}
else
cout << "Invalid node number!" << endl;
}
else {
node = first;
first = node->next;
first->prev = NULL;
node = NULL;
}
cout << "Node has been deleted successfully!" << endl;
}
};
int main() {
int op = 0;
linkedlist llist = linkedlist();
while(op != 4) {
cout << "1. Insert at the beginning\n2. Insert at the end\n3. Delete\n4. Display\n5. Exit" << endl;
cout << "Enter your choice:" << endl;
cin >> op;
switch(op) {
case 1:
llist.insert_beginning();
break;
case 2:
llist.insert_end();
break;
case 3:
llist.del();
break;
case 4:
llist.display();
break;
case 5:
cout << "Bye Bye!" << endl;
return 0;
break;
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}