-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_operations_doublyLinkedList
224 lines (211 loc) · 4.31 KB
/
all_operations_doublyLinkedList
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
struct node*prev;
};
struct node*head=NULL;
struct node*temp=new node;
void insertBeginning(int num)
{
node * temp = new node;
temp->prev = NULL;
temp->data = num;
node * temp1 = head;
head = temp;
temp->next = temp1;
}
void delEnd(){
node * temp = head;
while(temp->next!=NULL){
temp=temp->next;
}
node * prev = temp->prev;
prev->next = NULL;
delete temp;
}
void print()
{
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
void insertEnd(int n){
node * temp1 = new node;
node * temp = head;
temp1->data=n;
temp1->next=NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next = temp1;
temp1->prev=temp;
}
void createList()
{
cout<<"enter number of elements in list";
int n;
cin>>n;
int val;
for(int i =0;i<n;i++){
cout<<"enter value of node";
cin>>val;
node * temp = new node;
temp->data = val;
temp->next = NULL;
temp->prev= NULL;
if(head == NULL)
{
head = temp;
}
else{
node * temp1 = head;
while(temp1->next != NULL){
temp1 = temp1->next;
}
temp->prev = temp1;
temp1->next = temp;
}
}
}
void insertPos(int element,int n)
{
node * temp = head;
while(temp->data!=element){
temp=temp->next;
}
node * temp_n = new node;
temp_n->data=n;
temp_n->prev=temp;
temp_n->next=temp->next;
temp->next=temp_n;
}
void delPos(int n){
node * temp = head;
while(temp->data!=n)
{
temp = temp->next;
}
temp->prev->next=temp->next;
temp->next->prev = temp->prev;
delete temp;
}
void delBeginning(){
node * temp = head;
head = head->next;
head->prev=NULL;
delete temp;
}
void searchNode(int n)
{
node * temp = head;
int z = 1;
while(temp!=NULL){
if(temp->data == n){
cout<<"Element found at position:"<<z<<endl;
break;
}
temp=temp->next;
z++;
}
if(temp==NULL)
cout<<"not found"<<endl;
}
int main()
{
createList();
cout<<"The initial list is\n";
print();
cout<<endl;
cout<<endl;
while(1)
{
cout<<"Enter the choice:\n";
cout<<"1-Insertion at the beginning.\n";
cout<<"2-insertion at the end\n";
cout<<"3-Insertion in between nodes \n";
cout<<"4-Deletion from the beginning\n";
cout<<"5-Deletion from the end\n";
cout<<"6-Deletion of a specific node, mention the position\n";
cout<<"7-to print the position of a node\n";
cout<<"8-Display all the node values\n";
cout<<"9-Exit\n";
int ch;
int n;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the number to be inserted\n";
cin>>n;
insertBeginning(n);
cout<<"The updated list is\n";
print();
cout<<endl;
break;
case 2:
cout<<"Enter the number to be inserted\n";
cin>>n;
insertEnd(n);
cout<<"The updated list is\n";
print();
cout<<endl;
break;
case 3:
cout<<"Enter the element after which you would like to input\n";
int element;
cin>>element;
cout<<"Enter the number you want to input\n";
cin>>n;
insertPos(element,n);
cout<<"The updated list is\n";
print();
cout<<endl;
break;
case 4:
cout<<"The updated list is\n";
delBeginning();
print();
cout<<endl;
break;
case 5:
cout<<"The updated list is\n";
delEnd();
print();
cout<<endl;
break;
case 6:
cout<<"Enter the element you would like delete\n";
cin>>n;
delPos(n);
cout<<"The updated list is\n";
cout<<endl;
print();
break;
case 7:
cout<<"Enter the number you would like to search\n";
cin>>n;
searchNode(n);
cout<<endl;
break;
case 8:
cout<<"The list is\n";
print();
cout<<endl;
cout<<endl;
break;
case 9:
cout<<"Exiting\n";
exit(0);
break;
default:
cout<<"Sorry, incorrect choice "<<endl;
}
}
}