-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab8.c
More file actions
192 lines (191 loc) · 4.28 KB
/
Copy pathlab8.c
File metadata and controls
192 lines (191 loc) · 4.28 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int ssn;
char name[20], dept[20], desg[20], ph[20];
float sal;
} EMPL;
struct node
{
int ssn;
char name[20], dept[20], desg[20], ph[20];
float sal;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x = (NODE)malloc(sizeof(struct node));
if (x == NULL)
{
printf("Out of memory\n");
exit(0);
}
return x;
}
NODE insert_front(EMPL item, NODE first)
{
NODE temp;
temp = getnode();
temp->ssn = item.ssn;
strcpy(temp->name, item.name);
strcpy(temp->dept, item.dept);
strcpy(temp->desg, item.desg);
temp->sal = item.sal;
strcpy(temp->ph, item.ph);
temp->llink = temp->rlink = NULL;
if (first == NULL)
return temp;
temp->rlink = first;
first->llink = temp;
return temp;
}
NODE insert_rear(EMPL item, NODE first)
{
NODE temp, cur;
temp = getnode();
temp->ssn = item.ssn;
strcpy(temp->name, item.name);
strcpy(temp->dept, item.dept);
strcpy(temp->desg, item.desg);
temp->sal = item.sal;
strcpy(temp->ph, item.ph);
temp->llink = temp->rlink = NULL;
if (first == NULL)
return temp;
cur = first;
while (cur->rlink != NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE delete_front(NODE first)
{
NODE second;
if (first == NULL)
{
printf("Employee list is empty\n");
return NULL;
}
if (first->rlink == NULL)
{
printf("Employee details deleted: ssn=%d\n", first->ssn);
free(first);
return NULL;
}
second = first->rlink;
second->llink = NULL;
printf("Employee details: ssn=%d\n", first->ssn);
free(first);
return second;
}
NODE delete_rear(NODE first)
{
NODE cur, prev;
if (first == NULL)
{
printf("List is empty cannot delete\n");
return first;
}
if (first->rlink == NULL)
{
printf("Employee details deleted: ssn=%d\n", first->ssn);
free(first);
return NULL;
}
prev = NULL;
cur = first;
while (cur->rlink != NULL)
{
prev = cur;
cur = cur->rlink;
}
printf("Employee details deleted: ssn=%d\n", cur->ssn);
free(cur);
prev->rlink = NULL;
return first;
}
void display(NODE first)
{
NODE cur;
int count = 0;
if (first == NULL)
{
printf("Employee list is empty\n");
return;
}
cur = first;
while (cur != NULL)
{
printf("SSN-%d\nSALARY-%f\nNAME-%s\nDEPARTMENT-%s\nDESIGNATION-%s\nPHONE-%s\n", cur->ssn, cur->sal, cur->name, cur->dept, cur->desg, cur->ph);
cur = cur->rlink;
count++;
}
printf("Number of employees=%d\n", count);
}
int main()
{
NODE first;
int choice;
EMPL item;
first = NULL;
for (;;)
{
printf("\n1.Insert_front\t2.Insert_rear\n3.Delete_front\t4.Delete_rear\n5.Display\t6.Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("SSN:");
scanf("%d", &item.ssn);
printf("NAME:");
scanf("%s", &item.name);
printf("DEPARTMENT:");
scanf("%s", &item.dept);
printf("SALARY:");
scanf("%f", &item.sal);
printf("DESIGNATION:");
scanf("%s", &item.desg);
printf("PHONE:");
scanf("%s", &item.ph);
first = insert_front(item, first);
break;
case 2:
printf("SSN:");
scanf("%d", &item.ssn);
printf("NAME:");
scanf("%s", &item.name);
printf("DEPARTMENT:");
scanf("%s", &item.dept);
printf("SALARY:");
scanf("%f", &item.sal);
printf("DESIGNATION:");
scanf("%s", &item.desg);
printf("PHONE:");
scanf("%s", &item.ph);
first = insert_rear(item, first);
break;
case 3:
first = delete_front(first);
break;
case 4:
first = delete_rear(first);
break;
case 5:
display(first);
break;
case 6:
exit(0);
break;
}
}
return 0;
}