-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkListProgram54.c
288 lines (222 loc) · 7.55 KB
/
LinkListProgram54.c
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
#include<stdio.h>
#include<stdlib.h>
// Terminal Colors just to heighlight the nodes .. and its data
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define RESET "\x1B[0m"
// Structure of a particular node ...
typedef struct node{
int data;
struct node *nextNode;
}SLLNode;
// Function that creates a indivisual node and assigns feilds of the node
SLLNode *createNode(int num){
SLLNode *newNode = (SLLNode*)malloc(sizeof(SLLNode));
newNode->nextNode = NULL;
newNode->data = num;
return newNode;
}
// This function will count the nodes in the List
int countNodes(SLLNode **headNode, SLLNode **tailNode){
SLLNode *dummyNode = *headNode;
int count = 0;
for (; dummyNode != NULL && dummyNode -> nextNode != (*tailNode) -> nextNode; count++)
dummyNode = dummyNode -> nextNode;
return count;
}
// At the time of Termination of program this function is called that will free the memory of heap ...
void freeAllNodes(SLLNode **headNode, SLLNode **tailNode){
if (*headNode == NULL){return;}
if ((*headNode)->nextNode == NULL)
free(*headNode);
else{
while ((*headNode)->nextNode != *tailNode){
SLLNode *freeNode = *headNode;
*headNode = (*headNode)->nextNode;
free(freeNode);
}
}
}
// This function will count no. of tokens in a string on a delim
int getTokensCount(char *string, char delim){
int tokenCounter = 0;
char *strCpy = string;
int flag = 0;
char ch;
while((ch = *(strCpy++)) != '\0'){
if(ch == delim && flag){
tokenCounter += 1;
flag = 0;
}
if(ch != delim){
flag = 1;
}
if(ch == delim && *strCpy == '\0')
tokenCounter -= 1;
}
return tokenCounter + 1;
}
// This function will tokenise a string on a specific delimiter and return
// the pointer to int array converted of the string
int *tokeniseString(char *string, char delim){
int tokenCounter = 0;
int *tokenArray;
int *iTokenArr;
char *strCpy = string;
char buffer[10];
int iBuff = 0;
int flag = 0;
char ch;
tokenCounter = getTokensCount(string, delim);
tokenArray = (int*) malloc(tokenCounter * sizeof(int));
iTokenArr = tokenArray;
strCpy = string;
flag = 0;
while((ch = *(strCpy++)) != '\0'){
if(ch == delim && flag){
buffer[iBuff] = '\0';
*(iTokenArr++) = atoi(buffer);
iBuff = 0;
flag = 0;
}
if(ch != delim){
*(buffer + iBuff) = ch;
iBuff ++;
flag = 1;
}
if(*strCpy == '\0'){
buffer[iBuff] = '\0';
*(iTokenArr++) = atoi(buffer);
}
}
return tokenArray;
}
// This function will add a new Node to the end of the nodes ...
void addNewNode(SLLNode **headNode, SLLNode **tailNode, int num){
SLLNode *newNode = createNode(num);
if (*headNode == NULL){
*headNode = *tailNode = newNode;
(*tailNode) -> nextNode = *headNode;
}else{
SLLNode **dummyNode = headNode;
while((*dummyNode)->nextNode != (*tailNode) -> nextNode){
dummyNode = &(*dummyNode) -> nextNode;
}
(*dummyNode)->nextNode = newNode;
newNode -> nextNode = *headNode;
*tailNode = newNode;
}
}
// This function will take a input string and create a linked List from it ..
int createLinkedListFromNumberString(SLLNode **headNode,SLLNode **tailNode, char *string){
int *tokensFromString = tokeniseString(string, ' '); // Default delim is single space bar i.e. ' '
int tokenCount = getTokensCount(string, ' '); // Default delim is single space bar i.e. ' '
for (int i = 0; i<tokenCount; i++){
addNewNode(headNode, tailNode,tokensFromString[i]);
}
return 0;
}
// This function makes a copy of Circular linked list ...
int CllCopy(SLLNode **srcList,SLLNode **tailNodeOne, SLLNode **destList, SLLNode **tailNodeTwo){
if (*srcList == NULL) return -1;
SLLNode *destCpyList = *srcList;
SLLNode *dummyNode = createNode((*srcList)->data);
SLLNode *dummyCopy = dummyNode;
destCpyList = destCpyList -> nextNode;
do{
dummyNode -> nextNode = createNode(destCpyList->data);
dummyNode = dummyNode -> nextNode;
destCpyList = destCpyList -> nextNode;
}while(destCpyList != (*tailNodeOne) -> nextNode);
dummyNode -> nextNode = dummyCopy;
*tailNodeTwo = dummyNode;
*destList = dummyCopy;
return 0;
}
// This function performs inPlace reverse on the Linked List ...
int reverseList(SLLNode **headNode, SLLNode **tailNode){
SLLNode *nextNode = NULL;
SLLNode *prevNode = *headNode;
SLLNode *currentNode = *headNode;
*tailNode = *headNode;
do{
nextNode = currentNode -> nextNode; // store the nextNode
currentNode -> nextNode = prevNode; // reverse the pointer of the nextNode
prevNode = currentNode; // move pointer one step forWard ...
currentNode = nextNode;
}while(currentNode != *headNode);
(*headNode) -> nextNode = prevNode;
*headNode = prevNode;
return 0;
}
// This function will display the list of all the created nodes ...
void displayNodes(SLLNode **headNode, SLLNode **tailNode){
SLLNode *dummyNode = *headNode;
if (dummyNode == NULL){return;}
do{
printf("|\033[1;35m %d \033[0| \033[1;36m->\033[0m "RESET,dummyNode->data);
dummyNode = dummyNode->nextNode;
}while(dummyNode != *headNode);
}
// This function will whether CLList is palindrome or not...
int listPalindrome(SLLNode **headNodeOne, SLLNode **tailNodeOne){
SLLNode *reverseListNode = NULL;
SLLNode *reverseListNodeTailNode = NULL;
SLLNode *dummyNode = *headNodeOne;
CllCopy(headNodeOne,tailNodeOne,&reverseListNode,&reverseListNodeTailNode);
reverseList(&reverseListNode,&reverseListNodeTailNode);
int flag = 0;
do{
if (reverseListNode -> data != dummyNode -> data){
flag = 1;
break;
}
dummyNode = dummyNode -> nextNode;
reverseListNode = reverseListNode -> nextNode;
}while(dummyNode != *headNodeOne);
if (flag == 0)
return 0;
else
return -1;
}
// Driver code containing a menu to display options ...
void main(void){
int choice, retValue, data;
char str[256];
SLLNode *headNodeOne = NULL;
SLLNode *tailNodeOne = NULL;
do{
printf("\nmenu:\n");
printf("1] Add new Node (CLList 1)? : \n");
printf("2] Display Nodes ? : \n");
printf("3] CHeck CLList 1 Palindrome or not? :\n");
printf("4] Exit :\n");
printf("Enter a option --> ");
scanf("%d",&choice);
switch(choice){
case 1: printf("\nEnter nodes data seperated by delimiter(space key i.e ' ') :");
scanf(" %[^\n]",str);
printf("\n%s",str);
createLinkedListFromNumberString(&headNodeOne, &tailNodeOne, str);
break;
case 2: printf("\nCircular Link List One \n: ");
displayNodes(&headNodeOne, &tailNodeOne);
break;
case 3:
retValue = listPalindrome(&headNodeOne, &tailNodeOne);
if (retValue == -1)
printf(CYN"\t\tCLList NOT palindrome."RESET);
if (retValue == 0){
printf(CYN"\t\tCLList is palindrome."RESET);
printf("\nCircular Link List Two \n: ");
displayNodes(&headNodeOne, &tailNodeOne);
}
break;
case 4: freeAllNodes(&headNodeOne, &tailNodeOne);
exit(EXIT_SUCCESS);
break;
default:
printf(CYN"\t\tTry again ..."RESET);
}
}while(1);
}