-
Notifications
You must be signed in to change notification settings - Fork 0
/
Double Linked List
186 lines (165 loc) · 5.65 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
using namespace std;
class node {
public :
int data;
node *nextnode;
node *prevnode;
node() {
data = 0;
nextnode = NULL;
prevnode = NULL;
}
};
class list {
private :
node *head;
node *tail;
public :
list() {
head = NULL;
tail = NULL;
}
bool empty()
{
tail = NULL;
return (head==NULL);
}
void push_front()
{
node *ptr;
ptr = new node;
cout << "Enter value to insert : \n";
int v;
cin >> v;
ptr->data = v;
ptr->nextnode = NULL;
ptr->prevnode = NULL;
if (head!=NULL)
{
ptr->nextnode = head;
ptr->prevnode = NULL;
}
head = ptr;
cout << "\n \t" << ptr->data << "\t Pushed Successfully \n";
}
void push_back ()
{
node *ptr;
ptr = new node;
node *last;
last = head;
cout << "Enter value to insert : \n";
int v;
cin >> v;
ptr->nextnode = NULL;
if (empty())
{
ptr->prevnode = NULL;
head = ptr;
}
while (last!=NULL)
{
last = last->nextnode;
//last->nextnode = ptr; // remove if removing comment tag on the below comment tagged lines
//ptr->prevnode = last; // remove if removing comment tag on the below comment tagged lines
}
last->nextnode = ptr;
ptr->prevnode = last;
cout << "\n \t" << ptr->data << "\t Pushed Successfully \n";
}
void pop_front()
{
node *temp;
if (empty())
{
cout << "List Is Empty \n :";
}
temp = head;
head = head->nextnode;
cout << "\t" << temp->data << " \t Popped From Front Successfully \n";
delete temp;
}
/*void pop_back()
{
node *temp;
if (empty())
{
cout << "List Is Empty \n";
}
temp = head;
while (temp!=NULL)
{
temp = temp->nextnode;
tail = temp;
}
--temp;
cout << "\t";
delete temp;
}*/
/*void append()
{
code here
}*/
/*void deletenode()
{
code here
}*/
void count()
{
node *count;
int length;
count = head;
while (count!=NULL)
{
length++;
count = count->nextnode;
}
cout << "Number Of Nodes In The List Is : " << length << endl;
}
void display()
{
node *dp;
dp = head;
while (dp!=NULL)
{
cout << dp->data << "<====>";
dp = dp->nextnode;
}
cout << "NULL \n";
}
};
int main() {
list l;
int c;
while (1)
{
cout << "\n \t MENU : \n 1) PUSH : \n 2) POP : \n 3) DISPLAY : \n 4) SIZE : \n 5) EXIT : \n 6) PB :";
cout << "\n \n Enter desired action : \n";
cin >> c;
switch(c)
{
case 1 :
l.push_front();
break;
case 2 :
l.pop_front();
break;
case 3 :
l.display();
break;
case 4 :
l.count();
break;
case 6 :
l.push_back();
break;
case 5 :
return 0;
break;
default :
cout << "Enter valid choice : \n";
}
}
return 0;
}