-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked eval.c
242 lines (228 loc) · 4.49 KB
/
linked eval.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
typedef struct node *NODE;
NODE create_node();
NODE insert_end(NODE head);
void display_list(NODE head);
NODE insert_front(NODE head);
NODE delete_end(NODE head);
NODE delete_front(NODE head);
NODE insert_spc(NODE head);
NODE delete_spc(NODE head);
int main()
{
NODE head=NULL;
int count;
int ch;
while(1)
{
printf("\nSingly linked list menu\n");
printf("1:Insert_end 2:Insert_front 3:Delete_end 4:Delete_front 5:Display 6:Insert specific 7:Delete specific 8:Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:head=insert_end(head);
break;
case 2:head=insert_front(head);
break;
case 3:head=delete_end(head);
break;
case 4:head=delete_front(head);
break;
case 5:display_list(head);
break;
case 6:head=insert_spc(head);
break;
case 7:head=delete_spc(head);
break;
case 8:exit(0);
default:printf("Invalid selection\n");
break;
}
}
return 0;
}
NODE create_node()
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("Memory not allocated\n");
exit(0);
}
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->link=NULL;
return newnode;
}
NODE insert_end(NODE head)
{
NODE cur,newnode;
newnode=create_node();
if(head==NULL)
{
head=newnode;
}
else
{
cur=head;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=newnode;
}
return head;
}
void display_list(NODE head)
{
NODE cur=head;
if(head==NULL)
{
printf("Singly linked list is empty\n");
}
else
{
while(cur!=NULL)
{
printf("%d->",cur->data);
cur=cur->link;
}
}
}
NODE insert_front(NODE head)
{
NODE newnode;
newnode=create_node();
if(head==NULL)
{
head = newnode;
}
else
{
newnode->link = head;
head=newnode;
}
return head;
}
NODE delete_end(NODE head)
{
NODE prev , cur;
if(head==NULL)
{
printf("Singly linked list is empty\n");
}
else if(head->link==NULL)
{
printf("Deleted %d\n",head->data);
free(head);
head=NULL;
}
else
{
prev=NULL;
cur=head;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
printf("Deleted %d\n",cur->data);
free(cur);
}
return head;
}
NODE delete_front(NODE head)
{
NODE cur;
if(head==NULL)
{
printf("Singly linked list is empty\n");
}
else if(head->link==NULL)
{
printf("Deleted %d\n",head->data);
free(head);
head=NULL;
}
else
{
cur=head;
head=head->link;
printf("Deleted %d\n",cur->data);
cur->link=NULL;
free(cur);
}
return head;
}
int count_nodes(NODE head)
{
int count=0;
NODE cur;
if(head==NULL)
{
return count;
}
else
{
cur=head;
while(cur!=NULL)
{
count++;
cur=cur->link;
}
}
return count;
}
NODE insert_spc(NODE head)
{
NODE newnode,prev,cur;
int c,p,i;
newnode=create_node(head);
c=count_nodes(head);
printf("Enter position\n");
scanf("%d",&p);
if(p>1 && p<c)
{
prev=NULL;
cur=head;
for(i=0;i<p-1;i++)
{
prev=cur;
cur=cur->link;
}
prev->link=newnode;
newnode->link=cur;
}
return head;
}
NODE delete_spc(NODE head)
{
NODE prev,cur;
int c,p,i;
c=count_nodes(head);
printf("Enter position\n");
scanf("%d",&p);
if(p>1 && p<c)
{
prev=NULL;
cur=head;
for(i=0;i<p-1;i++)
{
prev=cur;
cur=cur->link;
}
prev->link=cur->link;
printf("Deleted %d",cur->data);
free(cur);
}
return head;
}