-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsll.c
192 lines (179 loc) · 2.92 KB
/
sll.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 30
#define MALLOC(p,s,t)\
if(!(p=(t)malloc(s))){\
fprintf(stderr,"Insufficient memory \n");\
exit(0);\
}
struct list
{
char USN[MAX];
char name[MAX];
char branch[MAX];
int sem;
char phno[MAX];
struct list *link;
};
typedef struct list *NODE;
NODE create(NODE,int);
NODE insfront(NODE);
NODE insrear(NODE);
NODE delfront(NODE);
NODE delrear(NODE);
void display(NODE);
void readdata();
char usn[MAX];
char sname[MAX];
char branc[MAX];
int se;
char phone[MAX];
void main()
{
int choice,done=0;
NODE fi;
fi=NULL;
int n;
while(!done)
{
printf("\n 1.Create \t 2.Insert front \t 3. Insert rear \t 4.Delete front \t 5.delete rear \t 6.display \t 7.Exit \n");
printf("Enter choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter no. of students \n");
scanf("%d",&n);
fi=create(fi,n);
break;
case 2:
readdata();
fi=insfront(fi);
break;
case 3:
readdata();
fi=insrear(fi);
break;
case 4:
fi=delfront(fi);
break;
case 5:
fi=delrear(fi);
break;
case 6:
display(fi);
break;
case 7:
done=1;
break;
default:
printf("Illegal entry \n");
break;
}
}
}
void readdata()
{
printf("Enter student USN: ");
scanf("%s",usn);
printf("Enter student name: ");
scanf("%s", sname);
printf("Enter branch: ");
scanf("%s",branc );
printf("Enter semester: ");
scanf("%d",&se);
printf("Enter phone number: ");
scanf("%s",phone);
}
NODE create(NODE first,int n)
{
int i;
if(first==NULL)
{
for(i=1;i<=n;i++)
{
printf("Enter student data %d \n",i);
readdata();
first=insfront(first);
}
return first;
}
else printf("List already has some student data \n");
}
NODE insfront(NODE first)
{
NODE q;
MALLOC(q,sizeof(struct list),NODE);
strcpy(q->USN,usn);
strcpy(q->name,sname);
strcpy(q->branch,branc);
strcpy(q->phno,phone);
q->sem=se;
q->link=first;
first=q;
return first;
}
NODE insrear(NODE first)
{
NODE q,t=first;
MALLOC(q,sizeof(struct list),NODE);
strcpy(q->USN,usn);
strcpy(q->name,sname);
strcpy(q->branch,branc);
strcpy(q->phno,phone);
q->sem=se;
q->link=NULL;
if(!first)
return q;
else
{
while(t->link)
t=t->link;
t->link=q;
}
}
NODE delfront(NODE first)
{
NODE te;
if(first == NULL)
printf("Can't delete,list is empty \n");
else
{
te=first;
first=first->link;
free(te);
}
return first;
}
NODE delrear(NODE first)
{
NODE cur,pred;
if(first == NULL)
{
printf("Can't delete,list is empty \n");
return first;
}
if(first->link==NULL)
return first=NULL;
cur=first;
while(cur->link!=NULL)
{
pred=cur;
cur=cur->link;
}
free(cur);
pred->link=NULL;
return first;
}
void display(NODE first)
{
int count =0;
while(first)
{
printf("%s\t%s\t%s\t%d\t%s\n",first->USN,first->name,first->branch,first->sem,first->phno);
first=first->link;
count++;
}
printf("count of nodes = %d \n",count);
}