-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram10.cpp
261 lines (258 loc) · 6.99 KB
/
Program10.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Design a menu driven Program in C++ for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept,Designation and salary. Perform
the following operations.
a. Create a double linked list of employees data.
b. Insert a new employee to the left of the node whose key value (employee name) is read
as an input.
c. Delete a node with given data, if it is found. Otherwise display appropriate error message */
#include <iostream>
using namespace std;
class Employee
{
string ssn;
string name;
string dept;
string design;
int salary;
public:
Employee(string ssn, string name, string dept, string design, int salary)
{
this->ssn = ssn;
this->name = name;
this->dept = dept;
this->design = design;
this->salary = salary;
}
friend class Node;
friend class DlinkedList;
};
class Node
{
Employee *emp;
Node *next;
Node *prev;
public:
Node(Employee *emp)
{
this->emp = emp;
next = prev = NULL;
}
friend class DlinkedList;
};
class DlinkedList
{
Node *head;
public:
DlinkedList()
{
head = NULL;
}
void create()
{
char choice;
do
{
string ssn, name, dept, desgn;
int salary;
cout << "Enter the SSN: ";
getline(cin, ssn);
cout << "Enter the name: ";
getline(cin, name);
cout << "Enter the department: ";
getline(cin, dept);
cout << "Enter the designation: ";
getline(cin, desgn);
cout << "Enter the salary: ";
cin >> salary;
Employee *emp_data = new Employee(ssn, name, dept, desgn, salary);
Node *newNode = new Node(emp_data);
if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
cout << "Enter 'y' for next data: ";
cin >> choice;
cin.ignore();
} while (choice == 'y');
display();
}
void insert_at_key(Employee *emp, string key)
{
if (head == NULL)
{
cout << "No Employee data";
}
else
{
Node *temp = head;
Node *flag_node;
while (temp != NULL && temp->emp->name != key)
{
flag_node = temp;
temp = temp->next;
}
if (temp == NULL)
{
cout << "Employee " << key << " is not present.";
}
else if (temp == head)
{
Node *newNode = new Node(emp);
head->prev = newNode;
newNode->next = head;
head = newNode;
cout << "Employee is Added Successfully!" << endl;
}
else
{
Node *newNode = new Node(emp);
newNode->next = flag_node->next;
newNode->prev = flag_node;
flag_node->next->prev = newNode;
flag_node->next = newNode;
cout << "Employee is Added Successfully!" << endl;
}
}
}
void delete_node(string key)
{
if (head == NULL)
{
cout << "No Employee Data" << endl;
return;
}
Node *temp = head;
while (temp != NULL && temp->emp->name != key)
{
temp = temp->next;
}
if (temp == NULL)
{
cout << "Employee with name " << key << " is not found" << endl;
}
else
{
if (temp == head)
{
// if only one node in a list and if it is to be deleted
if (head->next == NULL)
{
delete head;
head = NULL;
}
// if we have more nodes but want to delete head
else
{
head = head->next;
delete (head->prev);
head->prev = NULL;
}
}
else
{
// if temp is the middle node
if (temp->next && temp->prev)
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
// if temp is last node
else
{
temp->prev->next = NULL;
}
delete temp;
}
}
}
void display()
{
if (head == NULL)
{
cout << "No Employee List" << endl;
return;
}
cout << "Employee in list are: ";
Node *temp = head;
while (temp != NULL)
{
cout << temp->emp->name << "<<--->>";
temp = temp->next;
}
cout << endl;
cout << "---------------------------------------" << endl;
}
};
int main()
{
DlinkedList list;
cout << "\t**Menu Driven Program of Student Data**" << endl;
cout << "1.Create a double linked list of employees data" << endl;
cout << "2.Insert a node before specified employee name" << endl;
cout << "3.Delete a node with given data" << endl;
cout << "4.Display the employee list" << endl;
cout << "5.Exit" << endl;
while (1)
{
cout << "Enter a choice: ";
int choice;
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
{
list.create();
break;
}
case 2:
{
string ssn, name, dept, desgn;
int salary;
cout << "Enter the SSN: ";
getline(cin, ssn);
cout << "Enter the name: ";
getline(cin, name);
cout << "Enter the department: ";
getline(cin, dept);
cout << "Enter the designation: ";
getline(cin, desgn);
cout << "Enter the salary: ";
cin >> salary;
cin.ignore();
cout << "Enter the name of employee before which a new employee is to be added: ";
string key;
getline(cin, key);
Employee *emp_data = new Employee(ssn, name, dept, desgn, salary);
list.insert_at_key(emp_data, key);
break;
}
case 3:
{
cout << "Enter the name of the employee that is to be removed: ";
string name;
getline(cin, name);
list.delete_node(name);
break;
}
case 4:
list.display();
break;
case 5:
exit(0);
default:
cout << "Invalid Choice" << endl;
}
}
return 0;
}