-
Notifications
You must be signed in to change notification settings - Fork 1
/
all_list.c
282 lines (281 loc) · 9.52 KB
/
all_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include<stdlib.h>
#include<malloc.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node //Representation of a node
{
int num;
struct node *link; // struct node enna datatypeilekk point cheyyunna pointeran link
};
struct node *start=NULL;
int main()
{
int choice;
while(1){ // while 1 means while true
printf("\n LINKED LIST OPERATIONS \n");
printf("\n MENU \n");
printf("\n 1.Create \n");
printf("\n 2.Display \n");
printf("\n 3.Insert at the beginning \n");
printf("\n 4.Insert at the end \n");
printf("\n 5.Insert at specified position \n");
printf("\n 6.Delete from beginning \n");
printf("\n 7.Delete from the end \n");
printf("\n 8.Delete from specified position \n");
printf("\n 9.Exit \n");
printf("\n ------ \n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}
}
return 0; //since the function main has a return type int
}
void create() // Function to create a new node
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node)); // statement for dynamic memory allocation of a node
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t"); // For reading the value of a node
scanf("%d",&temp->num);
temp->link=NULL; // The link to the link node is as null
if(start==NULL) //Checking if it is first node
{
start=temp; //To set the starting address in start(basically linking the firt node to start address.
}
else
{
ptr=start;
while(ptr->link!=NULL) //link is the link field of the node
{
ptr=ptr->link; // For traversing b/w nodes
}
ptr->link=temp; //Linking existing node's address to the new node
}
}
void display()
{
struct node *ptr; //Declaring a node pointng to the structure variable
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start; //start contains the address of the first node
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->num );
ptr=ptr->link ; //traversing
}
}
}
void insert_begin() //inserting a node at the beginning of the linked list
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->num);
temp->link =NULL; //Setting the link of temp to null
if(start==NULL)
{
start=temp;
}
else
{
temp->link=start; //Linking the new node to start;where start is the address of the first node
start=temp;// removing start's link to the firt node
}
}
void insert_end() //inserting a node at the beginning of the list
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->num );
temp->link =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->link !=NULL)
{
ptr=ptr->link ;
}
ptr->link =temp; //THe link of the last node is set to temp
}
}
void insert_pos() //inserting a node at a specific function.
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->num);
temp->link=NULL;
if(pos==0) //if the user set the position to beginning
{
temp->link=start; //setting the link field of temp to start
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->link; //travering the linked list
if(ptr==NULL)
{
printf("\nPosition not found\n");
return;
}
}
temp->link =ptr->link; //Here we are setting the link field of the new node(temp) to position where the link field of node at the poition points too.
ptr->link=temp;
}
}
void delete_begin() //To delete from the beginning
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->link;
printf("\nThe deleted element is :%d\t",ptr->num);
free(ptr); //for removing the deleted node from memory
}
}
void delete_end() //To delete from the end
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(start->link==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->num);
free(ptr); //for deallocating the memory
}
else
{
ptr=start;
while(ptr->link!=NULL)
{
temp=ptr;
ptr=ptr->link;
}
temp->link=NULL; //setting the link field of the second last element to null
printf("\nThe deleted element is:%d\t",ptr->num);
free(ptr);
}
}
void delete_pos() //deleting a node from a specified position
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0) //if the position is entered as beginning
{
ptr=start;
start=start->link ;
printf("\nThe deleted element is:%d\t",ptr->num );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->link ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->link =ptr->link; //deleting the node
printf("\nThe deleted element is:%d\t",ptr->num );
free(ptr); // FInal deallocation
}
}
}