-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
233 lines (222 loc) · 4.92 KB
/
linked_list.c
File metadata and controls
233 lines (222 loc) · 4.92 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start = NULL, *newnode, *ptr, *temp;
int n;
void insert_start()
{
newnode = (struct node *)malloc(sizeof(struct node *));
if(newnode == NULL)
{
printf("Out of memory.");
return;
}
printf("Enter data: ");
scanf("%d", &n);
newnode -> data = n;
newnode -> next = start;
start = newnode;
}
void insert_end()
{
newnode = (struct node *)malloc(sizeof(struct node *));
if(newnode == NULL)
{
printf("Out of memory.");
return;
}
printf("Enter data: ");
scanf("%d", &n);
newnode -> data = n;
if(start == NULL)
{
newnode -> next = NULL;
start = ptr;
}
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = newnode;
newnode -> next = NULL;
}
}
void insert_after_node()
{
int val, count = 0;
if(start == NULL)
{
printf("List is empty.\n");
return;
}
newnode = (struct node *)malloc(sizeof(struct node *));
if(newnode == NULL)
{
printf("Out of memory.");
return;
}
printf("Enter value to insert: ");
scanf("%d", &n);
newnode -> data = n;
newnode -> next = NULL;
printf("Enter value of node to insert after: ");
scanf("%d", &val);
ptr = start;
while(ptr != NULL)
{
if(ptr -> data == val)
{
count = 1;
break;
}
ptr = ptr -> next;
}
if(count == 1)
{
newnode -> next = ptr -> next;
ptr -> next = newnode;
}
else
{
printf("Node not found.\n");
return;
}
}
void delete_start()
{
if(start == NULL)
{
printf("List is empty.\n");
return;
}
printf("Deleted data: %d", start -> data);
ptr = start;
start = start -> next;
free(ptr);
}
void delete_end()
{
if(start == NULL)
{
printf("List is empty.\n");
}
else if(start -> next == NULL)
{
printf("Deleted data: %d", start -> data);
free(start);
start = NULL;
}
else
{
ptr = start;
while(ptr -> next -> next != NULL)
{
ptr = ptr ->next;
}
printf("Deleted data: %d", ptr -> next -> data);
free(ptr -> next);
ptr -> next = NULL;
}
}
void delete_from_node()
{
if(start == NULL)
{
printf("List is empty.\n");
return;
}
int val, count = 0;
printf("Enter value of node to delete: ");
scanf("%d", &val);
ptr = start;
while (ptr != NULL)
{
if(ptr -> data == val)
{
count = 1;
break;
}
ptr = ptr -> next;
}
if(count == 1)
{
if(start -> data == val)
{
printf("Deleted data: %d", start -> data);
temp = start;
start = start -> next;
free(temp);
return;
}
ptr = start;
while (ptr -> next -> next != NULL)
{
if(ptr -> next -> data == val)
{
break;
}
ptr = ptr -> next;
}
printf("Deleted data: %d", ptr -> next -> data);
temp = ptr -> next;
ptr -> next = ptr -> next -> next;
free(temp);
return;
}
else
{
printf("No node found.\n");
return;
}
}
void display()
{
ptr = start;
if(ptr == NULL)
{
printf("List is empty.\n");
}
else
{
printf("Linked List:\t");
while (ptr != NULL)
{
printf("%d\t", ptr->data);
ptr = ptr -> next;
}
}
}
void main()
{
int ch;
do
{
printf("\n\t\t\t\t\tMENU\n");
printf("1. Insert at beginning\t\t2. Insert at end\t3. Insert after specified node\n");
printf("4. Delete from beginning\t5. Delete from end\t6. Delete from specified node\n");
printf("\t\t\t\t7. Display\t8. Exit\n");
printf("Enter choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert_start();
break;
case 2: insert_end();
break;
case 3: insert_after_node();
break;
case 4: delete_start();
break;
case 5: delete_end();
break;
case 6: delete_from_node();
break;
case 7: display();
break;
}
}while(ch >= 1 && ch <= 7);
}