-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularLinkedList.h
305 lines (284 loc) · 6.46 KB
/
circularLinkedList.h
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// root pointer points to the so called last node
typedef struct node
{
int info;
struct node *next;
}*NODEPTR;
// function prototypes
void initList(NODEPTR *); // to initalize list
void createList(NODEPTR *); // to create list
void displayList(NODEPTR); // to display list
int countNode(NODEPTR); // to count numer of nodes
void concatList(NODEPTR *,NODEPTR *); // to concatinate two lists
void insertFirst(NODEPTR *,int); // to insert a node at the begining
void insertLast(NODEPTR *,int); // to insert a node at the end
void insertAfter(NODEPTR,int); // to insert a node just after the node pointed to by the passed reference
void insertAfterInfo(NODEPTR *,int,int); // to insert a node after a match is found just after a given target
void deleteFirst(NODEPTR *); // to delete the first node
void deleteLast(NODEPTR *); // to delete the last node
void deleteAfter(NODEPTR); // to delete a node just after the node pointed to by the passed reference
int deleteInfo(NODEPTR *,int); // to delete all occurences of a given information
// function definitions
// to initalize list
void initList(NODEPTR *pList)
{
*pList = NULL;
}
// to create list
void createList(NODEPTR *pList)
{
NODEPTR newNode;
char choice;
do
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nMEMORY EXHAUSTED!!\n");
exit(1);
}
printf("\nEnter information... ");
scanf("%d",&newNode->info);
if(*pList == NULL)
newNode->next = newNode;
else
{
newNode->next = (*pList)->next;
(*pList)->next = newNode;
}
printf("\nDo you wish to continue? (Y/y for yes) ");
scanf(" %c",&choice);
*pList = newNode;
printf("%d\n",(*pList)->info);
}
while(choice =='y' || choice =='Y');
}
// to display list
void displayList(NODEPTR pList)
{
NODEPTR temp;
if(pList == NULL)
{
printf("\nNothing to display.\n");
return;
}
printf("\nThe List:\n");
temp = pList->next;
for(; temp != pList; temp = temp->next)
printf("%d->",temp->info);
printf("%d\n",pList->info);
}
// to count numer of nodes
int countNode(NODEPTR pList)
{
NODEPTR temp;
int count;
if(pList == NULL)
return 0;
count = 1;
for(temp = pList; temp->next != pList ; temp = temp->next)
++count;
return count;
}
// to concatinate two lists
void concatList(NODEPTR *pList1,NODEPTR *pList2)
{
NODEPTR temp;
if(*pList2 == NULL)
return;
if(*pList1 == NULL)
*pList1 = *pList2;
else
{
temp = (*pList2)->next;
(*pList2)->next = (*pList1)->next;
(*pList1)->next = temp;
*pList1 = *pList2;
}
}
// to insert a node at the begining
void insertFirst(NODEPTR *pList,int data)
{
NODEPTR newNode;
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nMEMORY EXHAUSTED!!\n");
exit(1);
}
newNode->info = data;
if(*pList == NULL)
{
newNode->next = newNode;
*pList = newNode;
}
else
{
newNode->next = (*pList)->next;
(*pList)->next = newNode;
}
}
// to insert a node at the last
void insertLast(NODEPTR *pList,int data)
{
NODEPTR newNode;
if(*pList == NULL)
insertFirst(pList,data);
else
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nMEMORY EXHAUSTED!!\n");
exit(1);
}
newNode->info = data;
newNode->next = (*pList)->next;
(*pList)->next = newNode;
*pList = newNode;
}
}
// to insert a node just after the node pointed to by the passed reference
void insertAfter(NODEPTR pList,int data)
{
NODEPTR newNode;
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nMEMORY EXHAUSTED!!\n");
exit(1);
}
newNode->info = data;
newNode->next = pList->next;
pList->next = newNode;
}
// to insert a node after a match is found just after a given target
void insertAfterInfo(NODEPTR *pList,int target,int data)
{
NODEPTR p, temp;
int status = 0; // not inserted
if(*pList == NULL)
{
printf("\nLIST EMPTY!!\n");
return;
}
p = temp = *pList;
do
{
if(p->info == target)
{
if(p == *pList) // match found at so-called last node
{
insertLast(pList,data); // root pointer will change
if(p->info == data) // avoid redundant entries (infinite loop)
p = p->next;
}
else // match at any other node
{
insertAfter(p,data);
if(p->info == data) // avoid redundant entries (infinite loop)
p = p->next;
}
status += 1; // change status if at least one match is found
}
p = p->next;
}
while(p!=temp);
if(!status)
printf("\nNo match found!\n");
else
printf("\n%d insertion(s) made.\n",status);
}
// to delete the first node
void deleteFirst(NODEPTR *pList)
{
NODEPTR delNode;
if(*pList == NULL)
{
printf("\nLIST EMPTY.\n");
return;
}
delNode = (*pList)->next;
if( delNode == delNode->next) // only one node
initList(pList);
else
(*pList)->next = delNode->next;
free(delNode);
}
// to delete the last node
void deleteLast(NODEPTR *pList)
{
NODEPTR delNode;
if(*pList == NULL)
{
printf("\nLIST EMPTY.\n");
return;
}
delNode = *pList;
if(delNode == delNode->next)
deleteFirst(pList);
else
{
for( ; delNode->next != *pList; delNode = delNode->next)
;
deleteAfter(delNode);
*pList = delNode;
}
}
// to delete a node just after the node pointed to by the passed reference
void deleteAfter(NODEPTR pList)
{
NODEPTR delNode;
delNode = pList->next;
pList->next = delNode->next;
free(delNode);
}
// to delete all occurences of a given information
int deleteInfo(NODEPTR *pList,int target)
{
NODEPTR delNode, temp, prevNode, toDel;
int status;
if(*pList == NULL)
{
printf("\nLIST EMPTY.\n");
return;
}
status = 0;
delNode = temp = *pList;
prevNode = NULL;
do
{
if(delNode->info == target)
{
if(prevNode == NULL) // match at beginning
{
//delNode = delNode->next;
deleteLast(pList);
if(*pList == NULL)
return;
prevNode = temp = *pList;
delNode = prevNode->next;
}
else // match elsewhere
{
toDel = prevNode; // delete after toDel
prevNode = delNode;
delNode = delNode->next;
deleteAfter(toDel);
}
status += 1;
}
else // no match
{
prevNode = delNode;
delNode = delNode->next;
}
}
while(delNode != temp);
if((*pList)->info == target)
{
deleteLast(pList);
++status;
}
return status;
}