Doubly Linked List With Header Nodes

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
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
	string name;
	float cgpa;
	int id;            	 //id of a student  string name;  	//name of a student  float cpga;    	 	  //cgpa of a student. 
	Student *next;       //address of the next available object. 
	Student *prev;      //address of the previous available object. 

public:

	Student(int _ID = -1, float _CGPA = -1, string _name = "-1", Student* _prev = NULL, Student* _next = NULL)
	{
		id = _ID;
		cgpa = _CGPA;
		name = _name;
		next = prev = NULL;
	}
	friend class StudentList;
};
class StudentList
{
private:
	Student *head;//start of the list 
	Student *cursor;//current item of the list 
	Student *tail; //last item of the list 
	int theSize;
public:
	StudentList()
	{
		theSize = 0;
		head = new Student;
		tail = new Student;
		head->next = tail;
		tail->prev = head;
		head->prev - tail;
		tail->next = head;
		cursor = NULL;
	}
	void insert(const Student& obj)
	{
		if (head->next == tail)
		{
			Student *p= new Student(obj);
			head->next = p;
			p->next = tail;
			tail->prev = p;
			p->prev = head;
			cursor = p;
		}
		else
		{
			Student* tmp = new Student(obj.id, obj.cgpa, obj.name);

			tail->prev->next = tmp;
			tmp->next = tail;
			tmp->prev = tail->prev;
			tail->prev = tmp;

			theSize++;
			cursor = tmp;
		}
	}
	~StudentList()
	{
		Student *tmp = head;
		while (tmp != tail)
		{
			Student *junk = tmp;
			tmp = tmp->next;
			delete junk;
		}
		delete tail;
	}
	void showStructureForward()
	{
		if (isEmpty())
		{
			cout << endl << " *** list empty *** " << endl << endl;
			return;
		}

		cursor = head;

		do
		{
			cursor = cursor->next;

			cout << "  ID :: " << cursor->id << endl;
			cout << "CGPA :: " << cursor->cgpa << endl;
			cout << "Name :: " << cursor->name << endl << endl;

		} while (cursor->next != tail);
	}
	bool isEmpty()
	{
		if (head->next == tail)
			return true;

		return false;
	}
	void showStructureReverse()
	{
		if (isEmpty())
		{
			cout << endl << " *** list empty *** " << endl << endl;
			return;
		}

		cursor = tail;

		do
		{
			cursor = cursor->prev;

			cout << "  ID :: " << cursor->id << endl;
			cout << "CGPA :: " << cursor->cgpa << endl;
			cout << "Name :: " << cursor->name << endl << endl;

		} while (cursor->prev != head);
	}
	void remove(int iD)
	{
		if (cursor->id == iD)
		{
			Student *tmp = cursor;
			cursor = tmp->next;
			tmp->prev->next = tmp->next;
			tmp->next->prev = tmp->prev;
			delete tmp;
			theSize--;
		}
		else if (cursor->id == iD && cursor->next == tail)
		{
			Student *tmp = cursor;
			cursor = head;
			tmp->prev->next = tmp->next;
			tmp->next->prev = tmp->prev;
			delete tmp;
			theSize--;
		}
		else
		{
			Student *tmp = head;
			while (tmp->next != tail)
			{
				if (tmp->id == iD)
				{
					tmp->prev->next = tmp->next;
					tmp->next->prev = tmp->prev;
					delete tmp;
					theSize--;
				}
			}
		}
	}
	void search(float Cgpa)
	{
		if (isEmpty())
		{
			cout << ":List is empty " << endl;
			return;
		}
		else if (cursor->cgpa == Cgpa)
		{
			cout << "CGPA is : " << cursor->cgpa;
			cout << "ID is : " << cursor->id;
			cout << "Name is : " << cursor->name;
		}
		else
		{
			Student *tmp = head;
			while (tmp->next != tail)
			{
				if (tmp->cgpa == Cgpa)
				{
					cout << "CGPA is : " << tmp->cgpa;
					cout << "ID is : " << tmp->id;
					cout << "Name is : " << tmp->name;
				}
				tmp = tmp->next;
			}
		}
	}
	Student getCursor()
	{
		
		return *cursor;
	}
	bool gotoPrior()
	{
		if (cursor != head)
		{
			cursor = cursor->prev;
			return true;
		}
		return false;
	}
	bool gotoNext()
	{
		if (cursor->next != tail)
		{
			cursor = cursor->next;
			return true;
		}
		return false;
	}
	void gotoEnd()
	{
		cursor = tail->prev;
	}
	void gotoBeginning()
	{
		cursor = head->next;
	}
	void replace(const Student& newStd)
	{
		bool find = false;

		if (isEmpty())
		{
			cout << "List is empty ";
			return;
		}
		else
		{
			Student *tmp = head;
			while (tmp->next != tail)
			{
				if (tmp->id == newStd.id)
				{
					tmp->id = newStd.id;
					tmp->cgpa = newStd.cgpa;
					tmp->name = newStd.name;
					find = true;
				}
				tmp = tmp->next;
			}
			if (find == false)
			{
				insert(newStd);
			}

		}
	}
};
Topic archived. No new replies allowed.