-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathDoubly Linked List.cpp
225 lines (202 loc) · 4.43 KB
/
Doubly Linked List.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
#include<iostream>
using namespace std;
struct Student
{
int age;
string name;
string id;
char sex;
Student *next;
Student *prev;
Student(int a,string na,string i,char s, Student *n=NULL, Student *p=NULL)
{name =na; age=a; id=i; sex=s; next =n; prev = p;}
};
// Create new node with structure Student
void create(Student * &t, string na, string i, char s, int a){
t= new Student(a, na, i, s);
}
// Register new Student
void insert(Student *&h, Student *&t)
{
Student *cur =h;
if(h == NULL)
h=t;
else {
while(cur->next)
{
cur = cur->next;
}
cur->next=t;
t->prev = cur;
}
}
// Register new student after a specific record
void insertAfter(Student *&h, Student *&t, string p)
{
Student *cur =h;
while(cur)
{
if(cur->id == p)
{
//t->prev=cur;
t->next = cur->next;
cur->next = t;
t->prev = cur;
//cout<<cur->next->name<<endl;
}
cur = cur->next;
}
}
//Display all students from last to end order
void reverse(Student *&h){
Student *tail=h;
while(tail->next !=NULL)
tail = tail->next;
cout<<"ID\tName\tAge\tSex\n";
cout<<"----------------------------\n";
do {
cout<<tail->id<<"\t"<<tail->name<<"\t"<<tail->age<<"\t"<<tail->sex<<"\n";
tail=tail->prev;
}while(tail->prev != NULL);
cout<<tail->id<<"\t"<<tail->name<<"\t"<<tail->age<<"\t"<<tail->sex<<"\n";
}
//Display all students record
void display(Student *&h){
Student *cur=h;
cout<<"ID\tName\tAge\tSex\n";
cout<<"----------------------------\n";
while(cur){
cout<<cur->id<<"\t"<<cur->name<<"\t"<<cur->age<<"\t"<<cur->sex<<"\n";
cur=cur->next;
}
}
//Display a specific student record
void displaySingle(Student *&h, string i){
Student *cur=h;
while(cur){
if(cur->id == i){
cout<<"ID: "<<cur->id<<"\n";
cout<<"Name: "<<cur->name<<"\n";
cout<<"Age: "<<cur->age<<"\n";
cout<<"Sex: "<<cur->sex<<"\n";
}
cur=cur->next;
}
}
//Delete a specific student record
void remove(Student *&h, string i)
{
Student *cur = h;
if(cur->id == i)
{
h = cur->next;
h->prev = NULL;
delete cur;
}
else{
while(cur){
if(cur->id==i)
{
if(cur->next == NULL)
{
cur->prev->next = NULL;
}
else{
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
}
delete cur;
}
cur=cur->next;
}
}
}
int main(){
//int data;
Student *temp, *head = NULL;
//student data variable declaration
int choice, ins_age;
string ins_id, ins_name, id_after;
char ins_sex;
cout<<"----- Menu -----\n";
cout<<endl<<"1. Add a Student : ";
cout<<endl<<"2. Add After a Specific Student : ";
cout<<endl<<"3. Delete a Student : ";
cout<<endl<<"4. Display All Students : ";
cout<<endl<<"5. Display Single Student : ";
cout<<endl<<"6. Display All Students Reverse : ";
cout<<endl<<"7. Exit : ";
cout<<"\n-----------------\n";
do{
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<endl<<"Enter student name: ";
getchar();
getline(cin,ins_name);
cout<<endl<<"Enter student id: ";
cin>>ins_id;
cout<<endl<<"Enter student sex: ";
cin>>ins_sex;
cout<<endl<<"Enter student age: ";
cin>>ins_age;
create(temp, ins_name, ins_id, ins_sex, ins_age);
insert(head, temp);
break;
}
case 2:
{
cout<<endl<<"After which student you want to add ? \n Enter Student ID : ";
getchar();
getline(cin, id_after);
cout<<endl<<"Register your student";
cout<<endl<<"Enter student name: ";
cin>>ins_name;
cout<<"\nEnter student id: ";
cin>>ins_id;
cout<<"\nEnter student sex: ";
cin>>ins_sex;
cout<<"\nEnter student age: ";
cin>>ins_age;
create(temp, ins_name, ins_id, ins_sex, ins_age);
insertAfter(head, temp, id_after);
break;
}
case 3:
{
cout<<endl<<"Enter student id. to be deleted: ";
cin>>ins_id;
remove(head, ins_id);
break;
}
case 4:
display(head);
break;
case 5:
{
cout<<endl<<"Enter a student id: ";
getchar();
getline(cin, ins_id);
displaySingle(head, ins_id);
break;
}
case 6:
{
reverse(head);
break;
}
case 7:
{
exit(1);
break;
}
default:
cout<<"Wrong choice! ";
break;
}
} while(choice != 7);
return 0;
}