-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab7.c
More file actions
218 lines (205 loc) · 3.64 KB
/
Copy pathLab7.c
File metadata and controls
218 lines (205 loc) · 3.64 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
/*Design, Develop and Implement a menu driven Program in C for the following
operations on
Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch,
Sem, PhNo.
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
*/
#include<stdio.h>
#include<stdlib.h>
//creation of structure for node
struct node
{
char usn[10],name[10],branch[10],phno[10];
int sem;
struct node *link;
};
typedef struct node *NODE;
NODE temp, FIRST=NULL;
//Function to create a NODE
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
x->link=NULL;
return x;
}
//Function to read the information which has to be stored in data field of a node
void read()
{
temp=getnode();
printf("Enter USN:");
scanf("%s",temp->usn);
printf("Enter Name:");
scanf("%s",temp->name);
printf("Enter Branch:");
scanf("%s",temp->branch);
printf("Enter Phone Number:");
scanf("%s",temp->phno);
printf("Enter Semester:");
scanf("%d",& temp->sem);
}
//a)Create a SLL of N Students Data by using front insertion.
void create_sll()
{
int i,n;
printf("Enter the number of students:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the details of student %d\n",i);
read();
if(FIRST==NULL)
{
FIRST=temp;
}
else
{
temp->link=FIRST;
FIRST=temp;
}
}
}
//b)Display the status of SLL and count the number of nodes in it
void display_count()
{
int count=0;
temp=FIRST;
printf("Student details:\n");
if(FIRST==NULL)
{
printf("Student detail is NULL. Count is Zero\n");
}
else
{
printf("\nUSN\tNAME\tBRANCH\tPH NO\tSEM\n");
while(temp->link!=NULL)
{
count++;
printf("%s\t%s\t%s\t%s\t%d\n",temp->usn,temp->name,temp->branch,temp->phno,temp->sem);
temp=temp->link;
}
count++;
printf("%s\t%s\t%s\t%s\t%d\n",temp->usn,temp->name,temp->branch,temp->phno,temp->sem);
printf("\n Student count:%d\n",count);
}
return;
}
//d)Insertion at front of SLL
void insert_front()
{
printf("Enter the details of students\n");
read();
if(FIRST==NULL)
{
FIRST=temp;
}
else
{
temp->link=FIRST;
FIRST=temp;
}
}
//c)Insertion at end of SSL
NODE insert_end()
{
NODE last=FIRST;
printf("Enter the details of student\n");
read();
if(FIRST==NULL)
{
FIRST=temp;
}
else
{
while(last->link!=NULL)
{
last=last->link;
}
last->link=temp;
}
return FIRST;
}
//d) deletion at front of SSL
void delete_front()
{
temp=FIRST;
if(FIRST==NULL)
{
printf("List is empty");
}
else
{
printf("Deleted elements is %s\n",temp->usn);
FIRST=FIRST->link;
free(temp);
}
return;
}
//c) deletion at end of SSL
void delete_end()
{
NODE last=NULL;
temp=FIRST;
if(FIRST==NULL)
{
printf("List is empty\n");
}
else if(FIRST->link==NULL)
{
printf("Deleted elemt is %s\n",temp->usn);
free(FIRST);
FIRST=NULL;
}
else
{
while(temp->link!=NULL)
{
last=temp;
temp=temp->link;
}
last->link=NULL;
printf("Deleted elemt is %s\n",temp->usn);
free(temp);
}
return;
}
//main Function
void main()
{
int choice;
while(1)
{
printf("-----------------MENU--------------------------------------\n");
printf("1. Create a SLL of N Students using front insertion\n");
printf("2. Display from Beginning\n");
printf("3. Insert at front\n");
printf("4. Insert at end\n");
printf("5. Delete at front\n");
printf("6. Delete at end\n");
printf("7. Exit\n");
printf("-----------------------------------------------------------\n");
printf("Enter choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1: create_sll();
break;
case 2: display_count();
break;
case 3: insert_front();
break;
case 4: insert_end();
break;
case 5: delete_front();
break;
case 6: delete_end();
break;
case 7: return;
default:printf("Invalid choice\n");
}
}
}