-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked_list.c
186 lines (181 loc) · 3.3 KB
/
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
#include<stdio.h>
#include<stdlib.h>
void display();
int in_b();
void in_e();
void in_bw();
void del_b();
void del_e();
void del_bw();
struct node *create();
struct node
{
int data;
struct node *link;
};
typedef struct node node1;
node1 * start = NULL;
void main()
{
int x;
printf("Singly Linked List\n MENU\n");
printf("1.Display\n2.Insertion at begining\n3.Insertion at end \n4.Insertion in between");
printf("\n5.Deletion at begning \n6.Deletion at end\n7.Deletion in between \n8.exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&x);
switch(x)
{
case 1:
display();
break;
case 2:
in_b();
break;
case 3:
in_e();
break;
case 4:
in_bw();
break;
case 5:
del_b();
break;
case 6:
del_e();
break;
case 7:
del_bw();
break;
case 8:
exit(0);
}
}
}
struct node *create()
{
node1 *nptr=((node1*)malloc(sizeof(node1)));
if(nptr==NULL)
{
printf("Memory Overflow");
return 0;
}
else
return nptr;
}
void display()
{
node1 *ptr=start;
printf("\nElements in the linked list are:\n");
while(ptr!=NULL)
{
printf("%d ",ptr-> data);
ptr=ptr->link;
}
}
int in_b()
{
int val;
node1 *nptr;
printf("Enter the element to be inserted:");
scanf("%d",&val);
nptr=create();
nptr->data=val;
if(start==NULL)
{
start=nptr;
nptr->link=NULL;
}
else
{
nptr->link=start;
start=nptr;
}
printf("Element inserted");
}
void in_e()
{
node1 *nptr=create(),*temp;
int val;
printf("Enter the element to be inserted:");
scanf("%d",&val);
nptr->data=val;
nptr->link=NULL;
temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=nptr;
printf("Element inserted");
}
void in_bw()
{
node1 *temp,*nptr=create();
int val,pos,i;
printf("Enter the position to be inserted:");
scanf("%d",&pos);
printf("Enter the element to be inserted:");
scanf("%d",&val);
nptr->data=val;
nptr->link=NULL;
temp=start;
if(pos==1)
{
nptr->link=start;
start=nptr;
}
else
{
for(i=1;i<pos-1;i++)
{
temp=temp->link;
}
}
nptr->link=temp->link;
temp->link=nptr;
printf("Element inserted");
}
void del_b()
{
node1 *temp;
if(start==NULL)
printf("List is empty!");
else
temp=start;
start=start->link;
free(temp);
printf("Element deleted");
}
void del_e()
{
node1 *temp,*prev;
temp=start;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
prev->link=NULL;
free(temp);
printf("Element deleted");
}
void del_bw()
{
node1 *temp,*pre;
int pos,i;
printf("Enter the position:");
scanf("%d",&pos);
temp=start;
if(pos==1)
start=start->link;
else
{
for(i=1;i<pos;i++)
{
pre=temp;
temp=temp->link;
}
pre->link=temp->link;
}
printf("Element deleted");
}