-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCircular linked list.cpp
200 lines (187 loc) · 4.09 KB
/
Circular linked list.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
#include <iostream>
using namespace std;
struct linklist
{
int item;
struct linklist *next;
};
typedef struct linklist node;
node *head=NULL;
int main()
{
int ch;
void create();
void display();
void insert_mid();
void insert_beg();
void del_beg();
void del_end();
void del_mid();
void search();
do{
printf("\nEnter the choice ");
cin>>ch;
switch(ch)
{
case 1: create(); break; // head node and insert nodes at the end of the list
case 2: display(); break;
case 3: insert_beg(); break;
case 4: insert_mid(); break;
case 5: del_beg();break;
case 6: del_end();break;
case 7: del_mid(); break;
case 8: search();break;
case 9: break;
}
}while(ch!=9);
return 0;
}
void create() //head=1000
{ // p=500
if(head==NULL) // 5|1000----> 10|2000 ----->20|3000 ----> 30|1000
{ // 15
head = new node;
cout<<"Enter the element ";
cin>>head->item;
head->next=head;
}
else //insert at the end of the list
{
node *p=head,*q;
while(p->next!=head)
{p=p->next;} //take the pointer to the last node
q = new node; //create the new node
cout<<"Enter the element ";
cin>>q->item;
q->next=head;
p->next=q;
}
}
void display()
{
node *p=head;
if(p==NULL)
{
cout<<"No element in the list";
return;
}
do
{
cout<<" "<<p->item;
p=p->next;
}while(p!=head);
}
void insert_beg()
{
node *p, *q=head;
p=new node;
cin>>p->item;
p->next=head;
while(q->next!=head)
q=q->next;
head=p;
q->next=head;
}
void insert_mid()
{
int num;
cout<< "Enter the item after which number you want to add the node";
cin>>num;
node *p=head;
while(p->item!=num)
{
p=p->next;
}
node *q;
q=new node;
cin>>q->item;
q->next=p->next;
p->next=q;
}
void del_beg()
{
int ele;
node *p=head, *q=head; //10 20 30
if(head==NULL){
cout<<"No element to be deleted";
return;
}
if(p->next==head) //for only one node
{
cout<<"deleted element= "<<p->item;
delete(p);
head=NULL;
}
else{
ele=p->item;
while(q->next!=head)
q=q->next;
head=p->next;
q->next=head;
delete(p);
cout<<"deleted element "<<ele;
}
}
void del_end()
{
if(head==NULL)
{
cout<<"No element to be deleted";
return;
}
node *p=head;
if(p->next==head) //for only one node
{
cout<<"deleted element= "<<p->item;
delete(p);
head=NULL;
}
else //more than one node
{
while(p->next->next!=head)
{
p=p->next;
}
int ele;
ele=p->next->item;
delete(p->next);
p->next=head;
cout<<"deleted element="<<ele;
}
}
void del_mid()
{
int num;
if(head==NULL)
{
cout<<"No element to be deleted";
return;
}
cout<<"Enter the middle element ";
cin>>num;// num=30
node *p=head, *q;
while(p->next->item!=num)
{
p=p->next;
}
q=p->next->next;
delete(p->next);
p->next=q;
}
void search()
{
int ele;
node *p=head;
cout<<"Enter the element";
cin>>ele;
do
{
if(p->item==ele)
{
cout<<"Found";
return;
}
p=p->next;
}while(p->next!=head);
cout<< "not found";
}