-
Notifications
You must be signed in to change notification settings - Fork 127
/
circular-linked-list-insertion-deletion.cpp
240 lines (219 loc) · 4.94 KB
/
circular-linked-list-insertion-deletion.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
class Circular_linkedList
{
Node *head;
public:
void Insertion_at_a_given_Position();
void Deletion_at_a_given_Position();
void Deletion_of_a_given_value();
void LinkedList_Creation();
void Display();
};
void Circular_linkedList::LinkedList_Creation()
{
Node *ptr = new Node;
int Number_of_nodes;
int information;
cout << "Enter the Number of Nodes you want to create: ";
cin >> Number_of_nodes;
for (int i = 0; i < Number_of_nodes; i++)
{
Node *currentNode = new Node;
cout << "Enter the data of the node " << i + 1 << " : ";
cin >> information;
if (i == 0)
{
head = ptr = currentNode;
}
currentNode->data = information;
currentNode->next = head;
ptr->next = currentNode;
ptr = currentNode;
}
ptr->next = head;
}
void Circular_linkedList ::Insertion_at_a_given_Position()
{
int data, position;
Node *temp = head;
Node *insertNode = new Node;
cout << "Enter the position you want to enter the element: ";
cin >> position;
cout << "Enter the data for the node:";
cin >> data;
insertNode->data = data;
for (int i = 0; i < position - 2; i++)
{
temp = temp->next;
}
if (position == 1)
{
Node *ptr = head;
ptr = ptr->next;
while (ptr->next != head) // it will place the ptr node just before the next node.
{
ptr = ptr->next;
}
insertNode->next = ptr->next;
ptr->next = insertNode;
head = ptr->next;
}
else
{
insertNode->next = temp->next;
temp->next = insertNode;
}
}
void Circular_linkedList::Display()
{
Node *temp = head;
if (head == NULL)
{
cout << "Linked List Empty";
}
else
{
cout << head->data << " ";
temp = temp->next;
while (temp != head)
{
cout << temp->data << " ";
temp = temp->next;
}
}
cout << endl;
}
void Circular_linkedList ::Deletion_at_a_given_Position()
{
int position;
Node *temp = head;
Node *forwardTemp = head;
cout << "Enter the given position: ";
cin >> position;
if (position == 1)
{
Node *ptr = head;
Node *temp = head;
ptr = ptr->next;
while (ptr->next != head)
{
ptr = ptr->next;
}
ptr->next = head->next;
head = head->next;
temp->next = NULL;
delete temp;
}
else
{
for (int i = 0; i < position - 2; i++)
{
temp = temp->next;
forwardTemp = temp->next;
}
temp->next = forwardTemp->next;
forwardTemp->next = NULL;
delete forwardTemp;
}
}
void Circular_linkedList ::Deletion_of_a_given_value()
{
int data,position,count=0;
bool flag = false;
cout << "Enter the data Which you want to delete: ";
cin >> data;
Node *temp = head;
temp = temp->next;
if (head->data == data)
{
Node *ptr = head;
Node *temp = head;
ptr = ptr->next;
while (ptr->next != head)
{
ptr = ptr->next;
}
ptr->next = head->next;
head = head->next;
temp->next = NULL;
delete temp;
}
else
{
Node *temp = head;
temp=temp->next;
Node *ptr=head;
while (temp!=head)
{
if(temp->data==data){
break;
}
temp=temp->next;
}
while(ptr->next!=temp){
ptr=ptr->next;
}
ptr->next=temp->next;
temp->next=NULL;
delete temp;
}
}
int main()
{
int choice;
Circular_linkedList operations;
do
{
cout << "1. Create the Linked List" << endl;
cout << "2. Display the Linked List" << endl;
cout << "3. Insert at a given position" << endl;
cout << "4. Deletion at a given position" << endl;
cout << "5. Deletion of a given value" << endl;
cout << "6. Exit";
cout << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
operations.LinkedList_Creation();
break;
}
case 2:
{
operations.Display();
break;
}
case 3:
{
operations.Insertion_at_a_given_Position();
break;
}
case 4:
{
operations.Deletion_at_a_given_Position();
break;
}
case 5:
{
operations.Deletion_of_a_given_value();
break;
}
case 6:
{
break;
}
default:
cout<<"WRONG CHOICE !!!!!!!!!!!!!!!!!!!!!";
break;
}
} while (choice != 6);
return 0;
}