-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.c
executable file
·291 lines (277 loc) · 5.85 KB
/
LinkList.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
289
290
291
/*
Link-List Program and has all Functions like:-
1)Insertion(at beginning, at end, after specific element, before specifc element)
2)Deletion(from beginning, from end, of specific element, or whole List at a go...)
3)Traversing(also, shows Addresse's of Nodes)
*/
#include<stdio.h>
#include<stdlib.h>
int start=0,last=0;
struct node* foundElement(int);
struct node* foundPredecessorElement(struct node*);
int findThatBloodyElement(int);
struct node{
int value;
struct node *next;
};
struct node *head = NULL;
struct node* createNode(){
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
return(temp);
}
void traverseList(){
struct node *temp;
if(head==NULL){
printf("List is Empty\n");
}
else{
temp = head;
printf("------Link List is------>\n");
while(temp->next != NULL){
printf("%d->", temp->value);
temp = temp->next;
}
printf("%d", temp->value);
}
}
void traverseListAddress(){
struct node *temp;
if(head==NULL){
printf("List is Empty\n");
}
else{
temp = head;
while(temp->next != NULL){
printf("\n----------------------");
printf("\n| %d|%p |------>", temp->value, temp);
printf("\n----------------------");
temp = temp->next;
}
printf("\n----------------------");
printf("\n| %d|%p(NULL) |", temp->value, temp);
printf("\n----------------------");
}
}
void insertNode(){
struct node *t, *t1;
t = createNode();
printf("Enter a Value\n");
scanf("%d", &t->value);
t->next = NULL;
if(head==NULL){ //Insertion at Beginning.
head = t;
start = t->value;
printf("Insertion Successfully\n");
}
else{ //Insertion at Last.
t1 = head;
while(t1->next != NULL){
t1 = t1->next;
}
t1->next = t;
last = t->value;
printf("Insertion Successfully\n");
}
}
void insertAfterElement(int value){
struct node *foundElementAddress, *temp1;
if(head==NULL){
printf("List is Empty\n");
}
else{
foundElementAddress = foundElement(value);
temp1 = createNode();
printf("Enter New Value\n");
scanf("%d", &temp1->value);
temp1->next = foundElementAddress->next;
foundElementAddress->next = temp1;
printf("Insertion Successfully\n");
last = temp1->value;
}
}
void insertBeforeElement(int value){
struct node *loc, *ptr, *ploc;
if(head==NULL){
printf("List is Empty\n");
}
else if(value==head->value){
printf("You are Inserting at Starting!, I am intelligently Developed\n");
ptr = createNode();
printf("Enter New Value\n");
scanf("%d", &ptr->value);
ptr->next = head;
head = ptr;
printf("Inserted Successfully at Starting!!!!\n");
start = ptr->value;
}
else{
loc = foundElement(value);
ploc = foundPredecessorElement(loc);
ptr = createNode();
printf("Enter New Value\n");
scanf("%d", &ptr->value);
ptr->next = loc;
ploc->next = ptr;
printf("Insertion Successfully\n");
}
}
void deleteFromBeginning(){
struct node *ptr;
if(head==NULL){
printf("List is already Empty!\n");
}
else{
ptr = head;
head = head->next;
free(ptr);
printf("Deletion Successfully\n");
}
}
void deleteFromEnd(){
struct node *ptr, *ploc;
if(head==NULL){
printf("List is Empty, Sir/Madam\n");
}
else{
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ploc = foundPredecessorElement(ptr);
ploc->next = NULL;
free(ptr);
printf("Deletion Successfully\n");
}
}
void deleteSpecificElement(int value){
struct node *loc, *ploc;
int found = 0;
found = findThatBloodyElement(value);
if(found==0){
printf("Element doesn't Exist, So Exiting...\n");
exit(0);
}
else if(start==value){
printf("You are Deleting Starting Element, I am... I am Intelligent!!\n");
deleteFromBeginning();
printf("Deleted!!\n");
}
else if(last==value){
printf("You are Deleting Last Element, As I said I am Intelligent!!\n");
deleteFromEnd();
printf("btw, Deleted Successfully\n");
}
else{
loc = foundElement(value);
ploc = foundPredecessorElement(loc);
ploc->next = loc->next;
free(loc);
printf("Deletion Successfully!!\n");
}
}
void deleteWholeList(){
struct node *ptr;
if(head==NULL){
printf("List is already Empty\n");
}
else{
while(head!=NULL){
ptr = head;
head = head->next;
free(ptr);
}
printf("Whole List is Empty, Now!!\n");
}
}
void main(){
int choice, value, delvalue;
while(1){
printf("\n1)Insertion\n2)Insertion After an Element\n3)Insert Before an Element\n4)Delete From Beginnning\n5)Delete From End\n6)Delete a Specific Element\n7)Traverse Link List\n8)Traverse List with Finding Addresses also\n9)Delete Whole List\n10)Exit\n");
scanf("%d", &choice);
if(choice==1) insertNode();
else if(choice==2){
printf("Enter the Value of that Element After you want to Insert your New Node\n");
scanf("%d", &value);
insertAfterElement(value);
}
else if(choice==3){
printf("Enter the Value of that Element before you want to Insert your New Node\n");
scanf("%d", &value);
insertBeforeElement(value);
}
else if(choice==4){
deleteFromBeginning();
}
else if(choice==5){
deleteFromEnd();
}
else if(choice==6){
printf("Enter the Element you want to Delete!!\n");
scanf("%d", &delvalue);
deleteSpecificElement(delvalue);
}
else if(choice==7){
traverseList();
}
else if(choice==8){
traverseListAddress();
}
else if(choice==9){
deleteWholeList();
}
else if(choice==10){
printf("Exiting....\n");
exit(0);
}
else{
printf("Wrong Choice, you Dump!: Exiting.....\n");
exit(0);
}
}
}//Main End!
struct node* foundElement(int value){
struct node *temp;
temp = head;
while(temp->next != NULL){
if(temp->value==value){
return(temp);
}
temp = temp->next;
}
if(temp->value==value){ //this is for if value is at Last.
printf("You are Inserting in Last, I am Intelligent than You!!!\n");
return(temp);
}
}
struct node* foundPredecessorElement(struct node *loc){
struct node *ploc, *temp;
ploc = head;
temp = head;
while(temp ->next !=NULL){
temp = temp->next;
if(temp==loc){
return(ploc);
}
else{
ploc = temp;
}
}
return(ploc);
}
int findThatBloodyElement(int value){
if(head==NULL){
printf("List is Empty;)\n");
return 0;
}
while(head->next != NULL){
if(head->value==value){
return 1;
}
head = head->next;
}
if(head->value==value)
return 1;
else
return 0;
}