-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_list.c
228 lines (212 loc) · 5.09 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
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//defining the structure
typedef struct nodetype
{
int info;
struct nodetype *next;
}node;
//function prototypes
void CreateEmptyList(node **head);
void TraverseInOrder(node *head);
void InsertAtBeginning(node **head, int item);
void TraverseInReverseOrder(node *head);
void InsertAtEnd(node **head, int item);
void InsertAfterElement(node *head, int item, int after);
node *Search(node *head, int item);
node *SearchInSorted(node *head, int item);
void DeleteFromBeginning(node **head);
void DeleteFromEnd(node **head);
void DeleteAfterElement(node *head, int item);
//main function
void main()
{
node *head;
int choice,element,after;
CreateEmptyList(&head);
printf("\nOperations available are:");
printf("\n1.Insert at beginning \n2.Traverse in order \n3.Traverse in reverse order\n4.Insert at end\n5.Search\n6.Insert after element\n7.Search in sorted list\n8.Delete from beginning\n9.Delete from end\n10.Delete after element\n");
while(1)
{
printf("Enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter element:");
scanf("%d",&element);
InsertAtBeginning(&head, element);
break;
case 2:
if(head==NULL)
printf("The list is empty");
else
TraverseInOrder(head);
break;
case 3:
if(head==NULL)
printf("The list is empty");
else
TraverseInReverseOrder(head);
break;
case 4:
printf("Enter element:");
scanf("%d",&element);
InsertAtEnd(&head,element);
break;
case 5:
printf("Enter element:");
scanf("%d",&element);
printf("%d",Search(head,element));
break;
case 6:
printf("Enter element after which to store:");
scanf("%d",&after);
printf("Enter element:");
scanf("%d",&element);
InsertAfterElement(head,element,after);
break;
case 7:
printf("Enter element:");
scanf("%d",&element);
printf("%d",SearchInSorted(head,element));
break;
case 8:
DeleteFromBeginning(&head);
break;
case 9:
DeleteFromEnd(&head);
break;
case 10:
printf("Enter element:");
scanf("%d",&element);
DeleteAfterElement(head,element);
break;
default:
exit(0);
}
}
}
//Function definitions
void CreateEmptyList(node **head)
{
*head = NULL;
}
void InsertAtBeginning(node **head, int item)
{
node *ptr;
ptr = (node*)malloc(sizeof(node));
ptr->info = item;
if(*head==NULL)
ptr->next=NULL;
else
ptr->next=*head;
*head=ptr;
}
void TraverseInOrder(node *head)
{
while(head!=NULL)
{
printf("%d\n", head->info);
head = head->next;
}
}
void TraverseInReverseOrder(node *head)
{
if(head->next!=NULL)
TraverseInReverseOrder(head->next);
printf("%d\n",head->info);
}
void InsertAtEnd(node **head, int item)
{
node *ptr, *loc;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
ptr->next=NULL;
if(*head==NULL)
*head=ptr;
else
{
loc=*head;
while(loc->next!=NULL)
loc=loc->next;
loc->next=ptr;
}
}
node *Search(node *head, int item)
{
while((head!=NULL) && (head->info!=item))
head=head->next;
return head;
}
node *SearchInSorted(node *head, int item)
{
while(head!=NULL)
{
if(item < head->info)
return NULL;
else if(head->info==item)
return head;
else
head=head->next;
}
return NULL;
}
void InsertAfterElement(node *head, int item, int after)
{
node *ptr, *loc;
loc=Search(head,after);
if(loc==(node*)NULL)
return;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
}
void DeleteFromBeginning(node **head)
{
node *ptr;
if(*head==NULL)
return;
else
{
ptr=*head;
*head=(*head)->next;
free(ptr);
}
}
void DeleteFromEnd(node **head)
{
node *ptr,*loc;
if(*head==NULL)
return;
else if((*head)->next==NULL)
{
ptr=*head;
*head=NULL;
free(ptr);
}
else
{
loc=*head;
ptr=(*head)->next;
while(ptr->next!=NULL)
{
loc=ptr;
ptr=ptr->next;
}
loc->next=NULL;
free(ptr);
}
}
void DeleteAfterElement(node *head, int item)
{
node *ptr,*loc;
loc=Search(head,item);
if(loc==(node*)NULL)
return;
ptr=loc->next;
loc->next=ptr->next;
free(ptr);
}