-
Notifications
You must be signed in to change notification settings - Fork 0
/
dll.c
201 lines (169 loc) · 5.34 KB
/
dll.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
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
// Employee structure
typedef struct Employee {
int SSN;
char Name[50];
char Department[50];
char Designation[50];
float Salary;
long PhNo;
struct Employee* prev;
struct Employee* next;
} Employee;
typedef struct Head {
int count;
struct Employee* llink;
struct Employee* rlink;
} HEAD;
// Function prototypes
void insfront(HEAD* head);
void insrear(HEAD* head);
void delfront(HEAD* head);
void delrear(HEAD* head);
void display(HEAD* head);
Employee* createEmployee(int SSN, const char* Name, const char* Department, const char* Designation, float Salary, long PhNo);
int main() {
int ch;
HEAD* head = (HEAD*)malloc(sizeof(HEAD));
head->count = 0;
head->llink = NULL;
head->rlink = NULL;
for (;;) {
printf("\n1. Insert Front\n2. Insert Rear\n3. Delete Front\n4. Delete Rear\n5. Display\n6. Exit\n");
scanf("%d", &ch);
switch (ch) {
case 1:
insfront(head);
break;
case 2:
insrear(head);
break;
case 3:
if (head->rlink == NULL)
printf("List Empty\n");
else
delfront(head);
break;
case 4:
if (head->rlink == NULL)
printf("List Empty\n");
else
delrear(head);
break;
case 5:
display(head);
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
// Insert at the front
void insfront(HEAD* head) {
int SSN;
char Name[50], Department[50], Designation[50];
float Salary;
long PhNo;
printf("Enter details such as SSN Name Department Designation Salary PhNo: ");
scanf("%d %s %s %s %f %ld", &SSN, Name, Department, Designation, &Salary, &PhNo);
Employee* newEmployee = createEmployee(SSN, Name, Department, Designation, Salary, PhNo);
newEmployee->next = head->rlink;
if (head->rlink != NULL) {
head->rlink->prev = newEmployee;
}
newEmployee->prev = NULL;
head->rlink = newEmployee;
if (head->llink == NULL) {
// If the list was empty
head->llink = newEmployee;
}
head->count++;
printf("Inserted at the front successfully.\n");
}
// Insert at the rear
void insrear(HEAD* head) {
int SSN;
char Name[50], Department[50], Designation[50];
float Salary;
long PhNo;
printf("Enter details such as SSN Name Department Designation Salary PhNo: ");
scanf("%d %s %s %s %f %ld", &SSN, Name, Department, Designation, &Salary, &PhNo);
Employee* newEmployee = createEmployee(SSN, Name, Department, Designation, Salary, PhNo);
newEmployee->prev = head->llink;
if (head->llink != NULL) {
head->llink->next = newEmployee;
}
newEmployee->next = NULL;
head->llink = newEmployee;
if (head->rlink == NULL) {
// If the list was empty
head->rlink = newEmployee;
}
head->count++;
printf("Inserted at the rear successfully.\n");
}
// Delete from the front
void delfront(HEAD* head) {
Employee* temp = head->rlink;
if (head->rlink == head->llink) {
// Only one node in the list
head->rlink = NULL;
head->llink = NULL;
} else {
head->rlink = temp->next;
temp->next->prev = NULL;
}
free(temp);
head->count--;
printf("Deleted from the front successfully.\n");
}
// Delete from the rear
void delrear(HEAD* head) {
Employee* temp = head->llink;
if (head->rlink == head->llink) {
// Only one node in the list
head->rlink = NULL;
head->llink = NULL;
} else {
head->llink = temp->prev;
temp->prev->next = NULL;
}
free(temp);
head->count--;
printf("Deleted from the rear successfully.\n");
}
// Display the list
void display(HEAD* head) {
if (head->rlink == NULL) {
printf("List is empty.\n");
return;
}
printf("Total number of records are %d\n", head->count);
printf("Data in the list:\n");
Employee* current = head->rlink;
while (current != NULL) {
printf("SSN: %d, Name: %s, Department: %s, Designation: %s, Salary: %.2f, PhNo: %ld\n",
current->SSN, current->Name, current->Department, current->Designation, current->Salary, current->PhNo);
current = current->next;
}
printf("\n");
}
// Helper function to create an Employee node
Employee* createEmployee(int SSN, const char* Name, const char* Department, const char* Designation, float Salary, long PhNo) {
Employee* newEmployee = (Employee*)malloc(sizeof(Employee));
newEmployee->SSN = SSN;
strcpy(newEmployee->Name, Name);
strcpy(newEmployee->Department, Department);
strcpy(newEmployee->Designation, Designation);
newEmployee->Salary = Salary;
newEmployee->PhNo = PhNo;
newEmployee->prev = NULL;
newEmployee->next = NULL;
return newEmployee;
}