-
Notifications
You must be signed in to change notification settings - Fork 1
/
singly linked list.c
239 lines (197 loc) · 2.48 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
//a C program to implement single linked list functions(insertion, deletion, display)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL, *ptr;
void insert_begin(int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
if (head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
void insert_end(int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=value;
if (head==NULL)
{
head=temp;
}
else
{
ptr=head;
while(ptr->next !=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->next=NULL;
}
}
void insert_after_specified_ele(int value,int ele)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=value;
if (head==NULL)
{
head=temp;
}
else
{
ptr=head;
while(ptr->data != ele)
{
ptr=ptr->next;
}
temp->next=ptr->next;
ptr->next=temp;
}
}
void delete_begin()
{
if(head==NULL)
{
printf("Empty list!");
}
else
{
ptr=head;
head=head->next;
ptr->next=NULL;
free(ptr);
}
}
void delete_end()
{
if(head==NULL)
{
printf("Empty list!");
}
else
{
struct node *p;
ptr=head;
while(ptr->next !=NULL)
{
p=ptr;
ptr=ptr->next;
}
p->next=NULL;
free(ptr);
}
}
void delete_middle(int ele)
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
struct node* p;
ptr=head;
while(ptr->data != ele)
{
p=ptr;
ptr=ptr->next;
}
p->next=ptr->next;
free(ptr);
}
}
void display()
{
if(head==NULL)
{
printf("List is empty!");
}
else
{
ptr=head;
while(ptr !=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
void main()
{
int value, ele, ch;
printf("\n 1. Insert at beginning \n 2. Insert at end \n 3. Insert after specified value \n 4.Delete at beginning \n 5. Delete at end\n 6. Delete in middle\n 7. Exit\n \n");
while(1)
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the desired value");
scanf("%d",&value);
insert_begin(value);
display();
break;
}
case 2:
{
printf("Enter the desired value");
scanf("%d",&value);
insert_end(value);
display();
break;
}
case 3:
{
printf("Enter the desired value");
scanf("%d",&value);
printf("Enter the specified element after which to insert:");
scanf("%d",&ele);
insert_after_specified_ele(value,ele);
display();
break;
}
case 4:
{
delete_begin();
display();
break;
}
case 5:
{
delete_end;
display();
break;
}
case 6:
{
printf("Enter the element that is to be deleted");
scanf("%d",&ele);
delete_middle(ele);
display();
break;
}
case 7:
{
exit(0);
}
}
}
}