forked from Mehr-un-nisa/cpp-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularLinkedList
286 lines (270 loc) · 5.46 KB
/
CircularLinkedList
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//////*Program to create a List*////////
#include<iostream>
using namespace std;
#include "Node.cpp"
// Creating list
class CirDoubList
{
Node *headNode;
Node *currentNode;
int size;
public:
//Constructor
CirDoubList()
{
headNode = 0;
currentNode = 0;
size = 0;
}
//insert/push Function
void insert(int n)
{
if (currentNode == 0)
{ //insert if stack is empty
Node *newNode = new Node();
newNode->setValue(n);
newNode->setNextNode(newNode);
newNode->setPrevPoin(newNode);
headNode = newNode;
currentNode = newNode;
size++;
}
else
{
if (currentNode->getNextNode() == 0)
{ //inserting node at the last of linked list
//insert if stack is not empty and current index at the last of the stack
Node *newNode = new Node();
newNode->setValue(n);
newNode->setNextNode(currentNode->getNextNode());
currentNode->setNextNode(newNode);
newNode->setPrevPoin(currentNode);
headNode->setPrevPoin(newNode);
currentNode = newNode;
size++;
}
else
{ //inserting node at the middle of linked list
//insert if stack is not empty and currentnode is not at last and to insert stack at middle
Node *newNode = new Node();
newNode->setValue(n);
newNode->setNextNode(currentNode->getNextNode());
newNode->setPrevPoin(currentNode);
(currentNode->getNextNode())->setPrevPoin(newNode);
currentNode->setNextNode(newNode);
currentNode = newNode;
size++;
}
}
}
//Fuction to get the value of node
int get()
{
return currentNode->getValue();
}
//function to get the size of linked list
int getSize()
{
return size;
}
//moving current node at start
void start()
{
currentNode = headNode;
}
//moving current node to neext node
void next()
{
if ((currentNode->getNextNode()) != NULL)
currentNode = currentNode->getNextNode();
}
//Functio to move back
void back()
{
if ((currentNode->getPrevNode()) != NULL)
currentNode = currentNode->getPrevNode();
}
//function to display list
void print()
{
start();
cout << "List of nodes" << endl;
for (int i = 0; i < getSize(); i++)
{
cout << "No " << i + 1 << " :";
cout << get();
next();
cout << endl;
}
}
//FUNCTION TO DELETE A NODE
void deletePtr(int v)
{
Node *ptr1 = new Node();
Node *ptr2 = new Node();
if (currentNode == 0)
{
cout << "Node is empty" << endl;
}
else
{
if (v > getSize() && v < getSize())
{
cout << "This node is not present in the list" << endl;
}
else
{
start();
for (int i = 1; i < v - 1; ++i)
{
next();
}
if (currentNode == headNode && v == 1)
{
cout << "For deleting head" << endl;
ptr1 = headNode;
currentNode = headNode = headNode->getNextNode();
headNode->setPrevPoin(ptr1->getPrevNode());
ptr2 = ptr1->getPrevNode();
ptr2->setNextNode(headNode);
ptr1->setNextNode(0);
ptr1->setPrevPoin(0);
--size;
}
else
{
ptr2 = headNode;
for (int i = 1; i < getSize(); ++i)
{
ptr2 = ptr2->getNextNode();
}
if(v==getSize())
{
ptr2 = ptr2->getPrevNode();
}
cout << "For DELTE AT END and FROM MEDIUM" << endl;
ptr1 = currentNode->getNextNode();
currentNode->setNextNode(ptr1->getNextNode());
headNode->setPrevPoin(ptr2);
ptr1->setNextNode(0);
ptr1->setPrevPoin(0);
--size;
}
}
}
}
//FUNCTION TO FIND THE SUM OF ALL NODES
int sum()
{
int s = 0;
start();
for (int i = 0; i < getSize(); i++)
{
s = s + get();
next();
}
return s;
}
//FUNCTION TO SEARCH NODES
Node* search(int v)
{
bool f = 0;
if (currentNode == 0)
{
cout << "List is empty" << endl;
return NULL;
}
else
{
start();
for (int i = 0; i < getSize(); i++)
{
if (v == get())
{
f = 1;
cout << "Found" << endl;
return currentNode->getNextNode();
}
next();
}
if (!f)
cout << "This value is ot present in any node" << endl;
return NULL;
}
}
//FUNCTION TO UPDATE VALUE OF NODE
void updateValue(int s, int v)
{
start();
for (int i = 0; i < s - 1; i++)
{
next();
}
currentNode->setValue(v);
}
//FUNCTION TO INSERT NODE AT LAST
void insertAtLast(int n)
{
start();
for (int i = 0; i < getSize() - 1; i++)
{
next();
}
Node *newNode = new Node();
newNode->setValue(n);
newNode->setNextNode(currentNode->getNextNode());
currentNode->setNextNode(newNode);
newNode->setPrevPoin(currentNode);
headNode->setPrevPoin(newNode);
currentNode = newNode;
size++;
}
//FUNCTION TO INSERT NODE AT BEGINNING
void insertAtBegin(int n)
{
Node *ptr = new Node();
ptr = headNode;
for(int i=1;i<getSize();++i)
{
ptr = ptr->getNextNode();
}
Node *newNode = new Node();
newNode->setValue(n);
newNode->setNextNode(headNode);
newNode->setPrevPoin(headNode->getPrevNode());
ptr->setNextNode(newNode);
headNode = newNode;
currentNode = newNode;
size++;
}
//FUNCTION TO INSERT NODE AFTER GIVEN INDEX
void insertNodePos(int n)
{
if (n > getSize() && n < getSize())
cout << "This index is not in the list" << endl;
start();
for (int i = 1; i < n - 1; i++)
{
next();
}
insert(2);
}
//FUNCTION TO INSERT NODE AFTER GIVEN VALUE
void insertNodeAfter(int s)
{
start();
bool f = 0;
for (int i = 1; i < getSize(); i++)
{
if (s == get())
{
cout << get();
insert(90);
f = 1;
}
next();
}
if (!f)
cout << "This nde is not present in this list" << endl;
}
};