-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task19.cpp
224 lines (201 loc) · 5.48 KB
/
Task19.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
/*
Creating Lists by using concepts of OOP
*/
#include<iostream>
#include<vector>
using namespace std;
class student {
public:
student() {
Year = 0;
Campus = ' ';
ID = 0;
CGPA = 0;
}
friend ostream& operator <<(ostream& os, student& S) {
os << "Student ID : " << S.ID << endl;
os << "Year : " << S.Year << endl;
os << "Campus : " << S.Campus << endl;
os << "CGPA : " << S.CGPA << endl;
return os;
}
friend istream& operator >>(istream& is, student& S) {
cout << "Enter ID : ";
is >> S.ID;
cout << "Enter Year : ";
is >> S.Year;
cout << "Enter Campus : ";
is >> S.Campus;
cout << "Enter CGPA : ";
is >> S.CGPA;
cout << endl;
return is;
}
bool operator ==(student obj)
{
if (this->Year == obj.Year && this->Campus == obj.Campus
&& this->ID == obj.ID && this->CGPA == obj.CGPA)
return true;
else
return false;
}
private:
int Year;
char Campus;
int ID;
double CGPA;
};
template<class T>
class listNode {
public:
bool operator < (listNode<T>& Obj) {
return ((*this.Data) < Obj.Data);
}
bool operator <= (listNode<T>& Obj) {
return ((*this.Data) <= Obj.Data);
}
bool operator > (listNode<T>& Obj) {
return ((*this.Data) > Obj.Data);
}
bool operator >= (listNode<T>& Obj) {
return ((*this.Data) >= Obj.Data);
}
bool operator == (listNode<T> Obj) {
return ((*this.Data) == Obj.Data);
}
T Data;
listNode<T>* next;
};
template<class U>
class D_List {
public:
D_List() {
Start = NULL;
totalRecords = 0;
}
~D_List() {
while (Start) {
listNode<U>* temp = Start;
Start = Start->next;
delete temp;
}
}
void showList() {
if (!Start) {
cout << "List is empty." << endl;
return;
}
cout << "List contents :- " << endl;
listNode<U>* current;
current = Start;
while (current != nullptr) {
cout << current->Data << endl;
current = current->next;
}
cout << "\n";
}
void insertSorted(listNode<U>& node) {
listNode<U>* newnode = new listNode<U>;
newnode->Data = node.Data;
newnode->next = Start;
Start = newnode;
totalRecords++;
}
bool deleteNode(listNode<U>& node) {
listNode<U>* present = Start;
listNode<U>* last = nullptr;
if (present->Data == node.Data) {
Start = present->next;
delete present;
totalRecords--;
return true;
}
while (present != nullptr) {
if (present->Data == node.Data) {
last->next = present->next;
delete present;
totalRecords--;
return true;
}
last = present;
present = present->next;
}
}
private:
listNode<U>* Start;
int totalRecords;
};
int main() {
D_List<student> DL;
D_List<int>D1;
char c;
cout << "Enter 'i' for integers list and 's' for students list : ";
cin >> c;
if (c == 'i' || c == 'I')
{
int choice;
do {
do {
cout << "1: Insert" << endl
<< "2: Delete" << endl
<< "3: Show List" << endl
<< "4: Exit" << endl << endl
<< "Enter Your Choice : ";
cin >> choice;
} while (1 > choice || choice > 4);
if (choice == 1) {
int x;
cout << "Enter number : ";
cin >> x;
listNode<int> I1;
I1.Data = x;
D1.insertSorted(I1);
}
else if (choice == 2) {
int x;
cout << "Enter number : ";
cin >> x;
listNode<int> I1;
I1.Data = x;
D1.deleteNode(I1);
}
else if (choice == 3) {
D1.showList();
}
} while (choice != 4);
}
if (c == 's' || c == 'S')
{
int choice;
do {
do {
cout << "1: Insert" << endl
<< "2: Delete" << endl
<< "3: Show List" << endl
<< "4: Exit" << endl << endl
<< "Enter Your Choice : ";
cin >> choice;
} while (1 > choice || choice > 4);
if (choice == 1) {
student s1;
cout << "Enter record of student :- " << endl;
cin >> s1;
listNode<student> St;
St.Data = s1;
DL.insertSorted(St);
}
else if (choice == 2) {
student s1;
cout << "Enter record of student :- " << endl;
cin >> s1;
listNode<student> St;
St.Data = s1;
DL.deleteNode(St);
}
else if (choice == 3) {
DL.showList();
}
} while (choice != 4);
}
return 0;
}