-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingly linked list operations.c
More file actions
277 lines (258 loc) · 5.14 KB
/
Singly linked list operations.c
File metadata and controls
277 lines (258 loc) · 5.14 KB
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
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#define MAX 10
typedef struct Student
{
int roll;
char name[MAX];
struct Student *next;
}NODE;
//typedef struct Student NODE;
NODE* deleteNodeByPosition( NODE *, int );
NODE* deleteNodeByName( NODE * );
NODE* reverseList( NODE * );
int countNodes( NODE* );
int main()
{
//initialize the list to be empty
NODE *start = NULL;
int choice;
int pos;
do
{
choice = displayMenuChoice();
switch( choice )
{
case 1 :printf("\nEnter position of the node to be deleted : ");
scanf("%d", &pos );
start = deleteNodeByPosition( start, pos );
break;
case 2 :start = deleteNodeByName( start );
break;
case 3 :start = reverseList( start );
break;
case 4 :printf("Number of nodes = %d", countNodes( start ));
break;
case 5 :printf("\nFInished with list operations !! ");
break;
default:printf("\nIncorrect choice !!! ");
}
printf("\nPress any key !! ");
getch();
}while( choice != 5 );
return;
}
int displayMenuChoice( void )
{
int choice;
printf("\n\t1] Delete node from the List ( By Position )");
printf("\n\t2] Delete node from the List ( By Name )");
printf("\n\t3] Reverse List ");
printf("\n\t4] Count Nodes in the List ");
printf("\n\t5] Exit");
printf("\n\n\tPlease enter your choice (1/2/3/4/5 ): " );
flushall();
scanf("%d",&choice);
return choice;
}
//Creates a list if it is not existing
//Appends new node if the list is existing
NODE* createList( NODE *start )
{
NODE *newNode; //used for creating new nodes
NODE *ptr; //used for traversing the list
//allocate memory for new node
newNode = ( NODE* )malloc( sizeof( NODE ) ) ;
//check whether allocation has failed
if( newNode == NULL )
{
printf("\nInsufficient memory!!!"\
"new node cannot be created");
}
else
{
//if allocation is successful accept data
newNode->next = NULL;
printf("\nPlease enter the roll number : ");
scanf("%d", &newNode->roll);
printf("\nPlease enter Name of the Student : ");
scanf("%s", newNode->name );
//check whether the list is empty
if( start == NULL )
{
//make new node the first node
start = newNode;
}
else
{
//point ptr to the begining of the list
ptr = start ;
//traverse to the end of the existing list
while( ptr->next != NULL )
{
ptr = ptr->next;
}
//add new node to the end of list
ptr->next = newNode;
}
}
//return list pointer
return start;
}
void displayList( NODE *ptr )
{
//checking if the list is empty
if( ptr == NULL )
{
printf("\nThe list is empty!!");
}
else
{
//traverse to the end of the list
while( ptr != NULL )
{
printf("\n Roll No = %d\tName : %s",ptr->roll, ptr->name);
ptr = ptr->next;
}
}
return;
}
int countNodes( NODE *ptr )
{
int count = 0;
//checking if the list is empty
if( ptr == NULL )
{
printf("\nThe list is empty!!");
}
else
{
//traverse to the end of the list
while( ptr != NULL )
{
count++;
ptr = ptr->next;
}
}
return count;
}
//deleting the node from the list by position
NODE* deleteNodeByPosition( NODE *start, int pos )
{
NODE *ptr;
NODE *temp;
int cnt;
//check whether list is empty
if( start == NULL )
{
printf("\nList is not Existing !");
return NULL;
}
//make ptr to point to the first node
ptr = start;
//ptr made to point to the node to be deleted
//temp made to point to node before the node to be
//deleted
cnt = 1;
while( cnt < pos && ptr != NULL )
{
temp = ptr;
ptr = ptr->next;
cnt++;
}
//check if ptr has traversed beyond the list
if( ptr == NULL )
{
printf("\nPosition out of the list");
}
else
{
//whether first node is to be deleted
if( pos == 1 )
{
// start = start->next;
start = ptr->next;
}
else
{
temp->next = ptr->next;
}
//release the memory ie delete node physically
free( ptr );
}
return start;
}
// search by student name to delete the node
NODE* deleteNodeByName( NODE *start )
{
NODE *ptr;
NODE *temp;
char tname[MAX];
//check if the list is empty
if( start == NULL )
{
printf("\nList is not Existing");
return NULL;
}
printf("\nPlease enter the name of the student to"\
" be deleted : ");
fflush(stdin);
gets( tname );
//making ptr point to first node
ptr = start;
while( ptr != NULL && (strcmpi(ptr->name,tname)!= 0 ) )
{
temp = ptr;
ptr = ptr->next;
}
// ptr has traversed beyond the list
if( ptr == NULL )
{
printf("\nThe student is not present in the list");
}
else
{
// if ptr is pointing to the first node
if( ptr == start )
{
//make start point to second node
start = ptr->next;
}
else
{
temp->next = ptr->next;
}
//delete node
free( ptr );
}
return start;
}
NODE* reverseList( NODE *start )
{
NODE *ptr;
NODE *temp;
NODE *ptr1;
// check if the list contains some node
if( start != NULL )
{
ptr = start;
temp = ptr->next;
ptr->next = NULL; //made 1st node as last node
while( temp != NULL )
{
ptr1 = temp->next;
temp->next = ptr;
ptr = temp;
temp = ptr1;
}
start = ptr;
}
else
{
printf("\nThe list is empty");
}
return start;
}