-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment4.cpp
218 lines (164 loc) · 3.97 KB
/
experiment4.cpp
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
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
void insertEnd(struct Node** start, int value)
{
if (*start == NULL)
{
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = new_node->prev = new_node;
*start = new_node;
return;
}
Node *last = (*start)->prev;
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = *start;
(*start)->prev = new_node;
new_node->prev = last;
last->next = new_node;
}
void insertBegin(struct Node** start, int value)
{
struct Node *last = (*start)->prev;
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = *start;
new_node->prev = last;
last->next = (*start)->prev = new_node;
*start = new_node;
}
void insertAfter(struct Node** start, int value1,int value2)
{
struct Node* new_node = new Node;
new_node->data = value1;
Node *p;
p=*start;
struct Node *temp = *start;
while (temp->data != value2)
temp = temp->next;
struct Node *next = temp->next;
temp->next = new_node;
new_node->prev = temp;
new_node->next = next;
next->prev = new_node;
}
void display(struct Node* start)
{
struct Node *temp = start;
while (temp->next != start)
{
cout<< temp->data;
temp = temp->next;
}
cout<< temp->data;
}
void deleteNode(struct Node **start, int key)
{
if (*start == NULL)
return;
struct Node *curr = *start, *prev_1 = NULL;
while (curr -> data != key)
{
if (curr->next == *start)
{
cout<<"\nList doesn't have node with value"<<key;
return;
}
prev_1 = curr;
curr = curr -> next;
}
if (curr -> next == *start && prev_1 == NULL)
{
(*start) = NULL;
return;
}
if (curr == *start)
{
prev_1 = (*start) -> prev;
*start = (*start) -> next;
prev_1 -> next = *start;
(*start) -> prev = prev_1;
}
else if (curr->next == *start)
{
prev_1 -> next = *start;
(*start) -> prev = prev_1;
}
else
{
struct Node *temp = curr -> next;
prev_1 -> next = temp;
temp -> prev = prev_1;
}
}
int searchList(struct Node* start, int search)
{
struct Node *temp = start;
int count=0,flag=0,value;
if(temp == NULL)
return -1;
else
{
while(temp->next != start)
{
count++;
if(temp->data == search)
{
flag = 1;
count--;
break;
}
temp = temp->next;
}
if(temp->data == search)
{
count++;
flag = 1;
}
if(flag == 1)
cout<<"\n"<<search <<" found at location "<<
count<<endl;
else
cout<<"\n"<<search <<" not found"<<endl;
}
}
int main()
{
struct Node* start = NULL;
int ch=1;
cout<<"1.Insert a new element at the beginning 2.end 3. in-between the given list"<<endl;
cout<<"4.Delete an existing element"<<endl;
cout<<"5.Search an element"<<endl;
cout<<"6.Display all the elements "<<endl;
int num,rep;
while(ch!=7)
{
cin>>ch;
switch(ch)
{
case 1:cin>>num;
insertBegin(&start,num);
break;
case 2:cin>>num;
insertEnd(&start,num);break;
case 3:cin>>num;cin>>rep;
insertAfter(&start,num,rep);break;
case 4:cout<<"enter the number to delete";
cin>>num;
deleteNode(&start,num);break;
case 5:cout<<"enter the number to search";
cin>>num;
searchList(start,num);break;
case 6:display(start);
break;
default:ch=7;
}
}
return 0;