C Program for Singly Linked List Operations

C Program for Singly Linked List Operations – Insert (Front, Pos, back), Delete, Search and Update.
Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT

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
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int sid;
char sname[25];
int ssem;
struct node *link;
}NODE;
 
NODE *InsFront(NODE *, int, char *, int);
NODE *InsBack(NODE *, int, char *, int);
NODE *InsPos(NODE *, int, char *, int,int);
NODE *DelNode(NODE *, int);
NODE *SrchUpdate(NODE *, int);
void Display(NODE *);
 
main()
{
NODE *start=NULL;		 /* Main Program */
int opn,id,sem,p,insopn;
char name[25];
do
{
clrscr();
printf("\n ### Linked List Operations ### \n\n");
printf("\n Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("Insertion at: Press 1->Front 2->Back 3->Pos ? ");
scanf("%d",&insopn);
printf("\n\nRead the Sid,Name, and Sem details ?");
scanf("%d%s%d",&id,name,&sem);
if( insopn == 1) start=InsFront(start,id,name,sem);
else if(insopn == 2) start=InsBack(start,id,name,sem);
else if(insopn == 3)
{
printf(" At What Position ? ");
scanf("%d",&p);
start=InsPos(start,id,name,sem,p);
}
break;
 
case 2: printf(" Read the Student Id of the Node to be deleted ? ");
scanf("%d",&id);
start=DelNode(start,id);
break;
case 3: printf(" Read the Student Id of the Node to be Searched ? ");
scanf("%d",&id);
start=SrchUpdate(start,id);
break;
case 4: printf(" Linked List is \n");
Display(start); break;
case 5: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n  Press a Key to Continue . . . ");
getch();
}while(opn != 5);
}
 
NODE *InsFront(NODE *st, int id, char *name, int sem)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
if( temp == NULL)
{ printf(" Out of Memory !! Overflow !!!");
return(st);
}
else
{
temp->sid=id;
strcpy(temp->sname,name);
temp->ssem=sem;
temp->link=st;
printf(" Node has been inserted at Front Successfully !!");
return(temp);
}
}
 
NODE *InsBack(NODE *st, int id, char *name, int sem)
{
NODE *temp,*t;
temp=(NODE *)malloc(sizeof(NODE));
if( temp == NULL)
{ printf(" Out of Memory !! Overflow !!!");
return(st);
}
else
{
temp->sid=id;
strcpy(temp->sname,name);
temp->ssem=sem;
temp->link=NULL;
if(st == NULL) return(temp);
else
{
t=st;
while( t->link != NULL)
t=t->link;
t->link=temp;
printf(" Node has been inserted at Back Successfully !!");
return(st);
}
}
}
 
NODE *InsPos(NODE *st, int id, char *name, int sem, int pos)
{
NODE *temp,*t,*prev;
int cnt;
temp=(NODE *)malloc(sizeof(NODE));
if( temp == NULL)
{ printf(" Out of Memory !! Overflow !!!");
return(st);
}
else
{
temp->sid=id;
strcpy(temp->sname,name);
temp->ssem=sem;
temp->link=NULL;
if(pos == 1)    /* Front Insertion */
{ temp->link=st;
return(temp);
}
else
{
t=st; cnt=1;
while( t != NULL && cnt != pos)
{
prev=t;
t=t->link;
cnt++;
}
if(t) /* valid Position  Insert new node*/
{
prev->link=temp;
temp->link=t;
}
else
printf(" Invalid Position !!!");
printf(" Node has been inserted at given Position  Successfully !!");
return(st);
}
}
}
 
NODE *DelNode(NODE *st, int id)
{
NODE *t,*prev;
if( st ==  NULL) { printf(" Underflow!!!"); return(st); }
else
{ t=st;
if(st->sid == id)    /* Front Deletion */
{ st=st->link;
t->link=NULL;
free(t);
return(st);
}
else
{
while( t != NULL && t->sid != id)
{
prev=t;
t=t->link;
}
if(t) /* node to be deleted  found*/
{
prev->link=t->link;
t->link=NULL;
free(t);
}
else
printf(" Invalid Student Id !!!");
return(st);
}
}
}
 
NODE *SrchUpdate(NODE *st, int id)
{
NODE *t;
if( st ==  NULL) { printf(" Empty List !!"); return(st); }
else
{  t=st;
while( t != NULL && t->sid != id)
{
t=t->link;
}
if(t) /* node to be Updated found*/
{
printf(" Node with Student Id %d found inthe List !\n",id);
printf(" Read the New Id,Name and Sem forthe Student\n");
scanf("%d%s%d",t->sid,t->sname,t->ssem);
}
else
printf(" Invalid Student Id !!!");
return(st);
}
}
 
void Display(NODE *st)
{ 
NODE *t;
if( st == NULL) printf("Empty List\n");
else
{
t=st;
printf("Start->");
while(t)
{
printf("[%d,%s,%d]->",t->sid,t->sname,t->ssem);
t=t->link;
}
printf("Null\n");
}
}
Ansten Lobo

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in