-
Notifications
You must be signed in to change notification settings - Fork 3
/
Singly Linked List.c
229 lines (221 loc) · 4.04 KB
/
Singly Linked List.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct node *front=NULL;
struct node *rear=NULL;
//initialise node
struct node
{
int key;
struct node *addr;
};
// insert an element at the beginning
void ins_at_beg(int k)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(front==NULL)
{
temp->key=k;
temp->addr=NULL;
front=rear=temp;
}
else
{
temp->key=k;
temp->addr=front;
front=temp;
}
}
//insert an element at the end
void ins_at_end(int k)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(front==NULL)
{
temp->key=k;
temp->addr=NULL;
front=rear=temp;
}
else
{
rear->addr=temp;
temp->key=k;
temp->addr=NULL;
rear=temp;
}
}
//insert the element at a particular position
void ins_at_pos(int k,int pos)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(pos==1)
{
ins_at_beg(k);
}
else
{
struct node *a;
a=front;
int i=1;
while(i<pos-1)
{
a=a->addr;
i++;
}
temp->addr=a->addr;
a->addr=temp;
temp->key=k;
}
}
//delete at beginning
void del_at_beg()
{
if(front==NULL)
{
printf("No nodes available!\n");
}
else if(front==rear)
{
front=rear=NULL;
free(front);
}
else
{
struct node *a;
a=front->addr;
front->addr=NULL;
free(front);
front=a;
}
}
//delete at end
void del_at_end()
{
if(front==NULL)
{
printf("No nodes available!\n");
}
else if(front==rear)
{
front=rear=NULL;
free(front);
}
else
{
struct node *a;
a=front;
while(a->addr!=rear)
{
a=a->addr;
}
a->addr=NULL;
free(rear);
rear=a;
}
}
//delete at a particular position
void del_at_pos(int pos)
{
if(front==NULL)
{
printf("No nodes available!\n");
}
else if(front==rear)
{
front=rear=NULL;
free(front);
}
else
{
int i=1;
struct node *a,*temp;
a=front;
while(i<pos-1)
{
a=a->addr;
i++;
}
temp=a->addr;
a->addr=a->addr->addr;
temp->addr=NULL;
free(temp);
}
}
//printing the list
void print()
{
if(front==NULL)
{
printf("No nodes\n");
}
else
{
struct node *a;
a=front;
printf("The elements are:\n");
while(a!=NULL)
{
printf("%d\n",a->key);
a=a->addr;
}
}
}
void main()
{
int a,b,c,pos,k,pos1,k1;
while(1)
{
printf("Select the option:\n");
printf("1.Insert\n2.Delete\n3.Print\n4.Exit\n");
scanf("%d",&a);
switch(a)
{
case 1: printf("Select the option:\n");
printf("1.1 Insert at beginning\n1.2 Insert at end\n1.3 Insert at position\n1.4 Exit\n");
scanf("%d",&b);
switch(b)
{
case 1: printf("Enter the element to be inserted:");
scanf("%d",&k);
ins_at_beg(k);
break;
case 2: printf("Enter the element to be inserted:");
scanf("%d",&k);
ins_at_end(k);
break;
case 3: printf("Enter the element and position:");
scanf("%d%d",&k,&pos);
ins_at_pos(k,pos);
break;
case 4: break;
default: printf("Error!!");
}
break;
case 2: printf("Select the option:\n");
printf("2.1 Delete at beginning\n2.2 Delete at end\n2.3 Delete at position\n2.4 Exit\n");
scanf("%d",&c);
switch(c)
{
case 1: del_at_beg();
break;
case 2: del_at_end();
break;
case 3: printf("Enter the position:");
scanf("%d",&pos1);
del_at_pos(pos1);
break;
case 4: break;
default: printf("Error!!");
}
break;
case 3: print();
break;
case 4: exit(0);
default : printf("Error!!");
}
}
}