-
Notifications
You must be signed in to change notification settings - Fork 0
/
listLib.h
executable file
·184 lines (172 loc) · 3.75 KB
/
listLib.h
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
//FREE TO CUSTOMIZE
/*
* =========================================================================================
* Name : listLib.h
* Author : Duc Dung Nguyen
* Email : [email protected]
* Copyright : Faculty of Computer Science and Engineering - Bach Khoa University
* Description : library for Assignment 1 - Data structures and Algorithms - Fall 2017
* =========================================================================================
*/
#ifndef A01_LISTLIB_H
#define A01_LISTLIB_H
#include <string>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
class DSAException
{
int _error;
string _text;
public:
DSAException() : _error(0), _text("Success") {}
DSAException(int err) : _error(err), _text("Unknown Error") {}
DSAException(int err, const char *text) : _error(err), _text(text) {}
int getError() { return _error; }
string &getErrorText() { return _text; }
};
template <class T>
struct L1Item
{
T data;
L1Item<T> *next;
L1Item() : next(NULL) {}
L1Item(T &a) : data(a), next(NULL) {}
};
template <class T>
class L1List
{
public:
L1Item<T> *_head; // The head pointer of linked list
L1Item<T> *_tail; //The end pointer of linked list
char request2nd[10]; //Special for 2nd request
size_t _size; // number of elements in this list
public:
L1List() : _head(NULL), _size(0) {};
void clean()
{
L1Item<T> *ptmp = _head;
while(ptmp)
{
L1Item<T> *ptmp2 = ptmp;
ptmp = ptmp->next;
delete ptmp2;
}
_head = _tail = NULL;
_size = 0;
}
bool isEmpty()
{
return _head == NULL;
}
size_t getSize()
{
return _size;
}
T &operator[](int i);
int push_back(T &a);
int insertHead(T &a);
int removeHead();
int removeLast();
void traverse(void (*op)(T &))
{
L1Item<T> *p = _head;
while (p)
{
op(p->data);
p = p->next;
}
}
void traverse(void (*op)(T &, void *), void *pParam)
{
L1Item<T> *p = _head;
while (p)
{
op(p->data, pParam);
p = p->next;
}
}
};
template <class T>
T &L1List<T>::operator[](int i)
{
L1Item<T> *ptmp = _head;
for (int index = 0; index < i; index++)
{
ptmp = ptmp->next;
}
return ptmp->data;
}
/// Insert item to the end of the list
template <class T>
int L1List<T>::push_back(T &a)
{
if (isEmpty())
{
_head = _tail = new L1Item<T>(a);
}
else
{
_tail->next = new L1Item<T>(a);
_tail = _tail->next;
}
_size++;
return 0;
}
/// Insert item to the front of the list
/// Return 0 if success
template <class T>
int L1List<T>::insertHead(T &a)
{
L1Item<T> *p = new L1Item<T>(a);
p->next = _head;
_head = p;
_size++;
return 0;
}
/// Remove the first item of the list
/// Return 0 if success
template <class T>
int L1List<T>::removeHead()
{
if (_head)
{
L1Item<T> *p = _head;
_head = p->next;
delete p;
_size--;
return 0;
}
return -1;
}
/// Remove the last item of the list
/// Return 0 if success
template <class T>
int L1List<T>::removeLast()
{
if (_head)
{
if (_head->next)
{
L1Item<T> *prev = _head;
L1Item<T> *pcur = prev->next;
while (pcur->next)
{
prev = pcur;
pcur = pcur->next;
}
delete pcur;
prev->next = NULL;
}
else
{
delete _head;
_head = NULL;
}
_size--;
return 0;
}
return -1;
}
#endif //A01_LISTLIB_H