1 /*
2 8. Develop a menu driven Program in C for the following operations on Doubly Linked List
3 (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,
4 Sal, PhNo
5 a. Create a DLL of N Employees Data by using end insertion.
6 b. Display the status of DLL and count the number of nodes in it.
7 c. Perform Insertion and Deletion at End of DLL.
8 d. Perform Insertion and Deletion at Front of DLL.
9 e. Demonstrate how this DLL can be used as Double Ended Queue.
10 f. Exit.
11 */
12
13 #include<string.h>
14 #include<stdio.h>
15 #include<stdlib.h>
16 int count=0;
17 struct employee
18 {
19 char ssn[15],name[20],dept[10],desg[20],phno[15];
20 int sal;
21 struct employee *rl,*ll;
22 };
23 typedef struct employee EMPLOYEE;
24 EMPLOYEE *first=NULL,*newnode,*prev,*temp;
25
26 void createNode()
27 {
28 newnode =(EMPLOYEE *)malloc(sizeof(EMPLOYEE));
29 printf("\n Enter fields: SSN, Name, Dept, Designation, Sal, PhNo respectively. \n");
30 scanf("%s%s%s%s%d%s",newnode->ssn,newnode->name,newnode->dept,newnode->desg,&newnode
->sal,newnode->phno);
31 newnode->rl = NULL;
32 newnode->ll = NULL;
33 }
34
35 void insertBeg()
36 {
37 createNode();
38 if (first == NULL)
39 first = newnode;
40 else
41 {
42 newnode -> rl = first;
43 first -> ll = newnode;
44 first = newnode;
45 }
46 }/*insertBeg() */
47
48
49
50 void deleteBeg()
51 {
52 if (first == NULL)
53 printf("\nThe list is empty.\n");
54 else
55 {
56 temp = first;
57 first = first->rl;
58 first->ll = NULL;
59 printf("\n%-20s %-20s %-20s %-20s %-20d %-20s is deleted\n ",temp->ssn,temp->name
,temp->dept,temp->desg,&temp->sal,temp->phno);
60 free(temp);
61 }
62 }/*deleteBeg() */
63
64
65
66
67
68
69
70
71
72
73 void insertEnd()
74 {
75 createNode();
76 if (first == NULL)
77 first = newnode;
78 else
79 {
80 temp=first;
81 while (temp->rl != NULL)
82 temp=temp->rl;
83 temp->rl = newnode;
84 newnode->ll = temp;
85 }
86 }/*insertEnd()*/
87
88
89
90
91
92
93
94
95
96
97
98 void deleteEnd()
99 {
100 if (first == NULL)
101 printf("\nThe list is empty.\n");
102 else{
103 if (first->rl == NULL)
104 {
105 printf("%-20s %-20s %-20s %-20s %-20d %-20s is deleted",first->ssn,first->name,
first->dept,first->desg,&first->sal,first->phno);
106 free(first);
107 first = NULL;
108 }
109 else
110 {
111
112
113 temp = first;
114 while (temp->rl != NULL)
115 {
116 prev = temp;
117 temp = temp -> rl;
118 }
119 prev->rl = NULL;
120 printf("\n%-20s %-20s %-20s %-20s %-20d %-20s is deleted\n",temp->ssn,temp->name,
temp->dept,temp->desg,&temp->sal,temp->phno);
121 free(temp);
122 }
123 }/*else*/
124 }/* deleteEnd() */
125
126
127
128
129
130
131
132 void display()
133 {
134 count = 0;
135 if (first == NULL)
136 printf("\nThe list is empty.\n");
137 else
138 {
139 printf("\nDoubly Linked List Status:\n");
140 printf("\nSSN\t\tName\t\tDept\t\tDesignation\t\tSalary\t\tPhone No:\t\t\n");
141 temp = first;
142 while (temp != NULL)
143 { count++;
144 printf("%-16s %-16s %-16s %-16s %-16d %-16s\n",temp->ssn, temp->name, temp->
dept, temp->desg, temp->sal, temp->phno);
145 temp = temp->rl;
146 }
147 printf("Total number of Nodes: %d\n", count);
148 }/*else*/
149 }/*display()*/
150
151
152
153 void creatlist()
154 {
155 int n,i;
156 printf("Enter number of employees: ");
157 scanf("%d", &n);
158 for(i = 0; i < n; i++)
159 insertEnd();
160 }
161 void dqueue()
162 {
163 int choice;
164 first = NULL;
165 do{
166 printf("Queue operation");
167 printf("\n [Link] begining\n [Link] begining\n [Link] rear\n [Link]
rare\n [Link]\n [Link]\n");
168 printf("Enter choice..");
169 scanf("%d",&choice);
170 switch (choice)
171 {
172 case 1: insertBeg();break;
173 case 2: deleteBeg(); break;
174 case 3: insertEnd();break;
175 case 4: deleteEnd();break;
176 case 5: display();
177 }
178 }while(choice<6);
179 }/*dequeue*/
180
181
182
183 void main()
184 {
185 int ch,n,i;
186 do{
187 printf("\n [Link] a list employees\n [Link]\n [Link] begining\n [Link]
begining\n [Link] End\n [Link] End\n [Link]\[Link]\n");
188 printf("\n Enter choice : ");
189 scanf("%d",&ch);
190 switch (ch)
191 {
192 case 1: creatlist();break;
193 case 2: display(); break;
194 case 3: insertBeg();break;
195 case 4: deleteBeg();break;
196 case 5: insertEnd();break;
197 case 6: deleteEnd();break;
198 case 7: dqueue();
199 }
200 }while(ch<8);
201 }
202
203
204
205
206
207 /*
208
209 [Link] a list employees
210 [Link]
211 [Link] begining
212 [Link] begining
213 [Link] End
214 [Link] End
215 [Link]
216 [Link]
217
218 Enter choice : 1
219 Enter number of employees: 1
220
221 Enter fields: SSN, Name, Dept, Designation, Sal, PhNo respectively.
222 123-45 John ISE Teacher 85000
08185252748
223
224 [Link] a list employees
225 [Link]
226 [Link] begining
227 [Link] begining
228 [Link] End
229 [Link] End
230 [Link]
231 [Link]
232
233 Enter choice : 3
234
235 Enter fields: SSN, Name, Dept, Designation, Sal, PhNo respectively.
236 246-80 Gopi Doctor Dental 68000
08185265874
237
238 [Link] a list employees
239 [Link]
240 [Link] begining
241 [Link] begining
242 [Link] End
243 [Link] End
244 [Link]
245 [Link]
246
247 Enter choice : 5
248
249 Enter fields: SSN, Name, Dept, Designation, Sal, PhNo respectively.
250 135-79 Raju Engineer Developer 90000
08185254857
251
252 [Link] a list employees
253 [Link]
254 [Link] begining
255 [Link] begining
256 [Link] End
257 [Link] End
258 [Link]
259 [Link]
260
261 Enter choice : 2
262
263 Doubly Linked List Status:
264
265 SSN Name Dept Designation Salary
Phone No:
266 246-80 Gopi Doctor Dental 68000
08185265874
267 123-45 John ISE Teacher 85000
08185252748
268 135-79 Raju Engineer Developer 90000
08185254857
269 Total number of Nodes: 3
270
271 [Link] a list employees
272 [Link]
273 [Link] begining
274 [Link] begining
275 [Link] End
276 [Link] End
277 [Link]
278 [Link]
279
280 Enter choice : 4
281
282 246-80 Gopi Doctor Dental
1484027376 08185265874 is deleted
283
284 [Link] a list employees
285 [Link]
286 [Link] begining
287 [Link] begining
288 [Link] End
289 [Link] End
290 [Link]
291 [Link]
292
293 Enter choice : 6
294
295 135-79 Raju Engineer Developer
1484053408 08185254857 is deleted
296
297 [Link] a list employees
298 [Link]
299 [Link] begining
300 [Link] begining
301 [Link] End
302 [Link] End
303 [Link]
304 [Link]
305
306 Enter choice : 7
307 Queue operation
308 [Link] begining
309 [Link] begining
310 [Link] rear
311 [Link] rare
312 [Link]
313 [Link]
314 Enter choice..1
315
316 Enter fields: SSN, Name, Dept, Designation, Sal, PhNo respectively.
317 864-20 Rani Doctor Surgeon 72000
08185258745
318 Queue operation
319 [Link] begining
320 [Link] begining
321 [Link] rear
322 [Link] rare
323 [Link]
324 [Link]
325 Enter choice..4
326 864-20 Rani Doctor Surgeon
1484027376 08185258745 is deletedQueue operation
327 [Link] begining
328 [Link] begining
329 [Link] rear
330 [Link] rare
331 [Link]
332 [Link]
333 Enter choice..5
334
335 The list is empty.
336 Queue operation
337 [Link] begining
338 [Link] begining
339 [Link] rear
340 [Link] rare
341 [Link]
342 [Link]
343 Enter choice..6
344
345 [Link] a list employees
346 [Link]
347 [Link] begining
348 [Link] begining
349 [Link] End
350 [Link] End
351 [Link]
352 [Link]
353
354 Enter choice : 8
355 */