-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlistLib.h
158 lines (135 loc) · 3.57 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
/*
* =========================================================================================
* 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> *pNext;
L1Item() : pNext(NULL) {}
L1Item(T &a) : data(a), pNext(NULL) {}
};
template <class T>
class L1List {
L1Item<T> *_pHead;// The head pointer of linked list
size_t _size;// number of elements in this list
public:
L1List() : _pHead(NULL), _size(0) {}
~L1List();
void clean();
bool isEmpty() {
return _pHead == NULL;
}
size_t getSize() {
return _size;
}
T& at(int i);
T& operator[](int i);
bool find(T& a, int& idx);
int insert(int i, T& a);
int remove(int i);
int push_back(T& a);
int insertHead(T& a);
int removeHead();
int removeLast();
void reverse();
void traverse(void (*op)(T&)) {
L1Item<T> *p = _pHead;
while (p) {
op(p->data);
p = p->pNext;
}
}
void traverse(void (*op)(T&, void*), void* pParam) {
L1Item<T> *p = _pHead;
while (p) {
op(p->data, pParam);
p = p->pNext;
}
}
};
/// Insert item to the end of the list
/// Return 0 if success
template <class T>
int L1List<T>::push_back(T &a) {
if (_pHead == NULL) {
_pHead = new L1Item<T>(a);
}
else {
L1Item<T> *p = _pHead;
while (p->pNext) p = p->pNext;
p->pNext = new L1Item<T>(a);
}
_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->pNext = _pHead;
_pHead = p;
_size++;
return 0;
}
/// Remove the first item of the list
/// Return 0 if success
template <class T>
int L1List<T>::removeHead() {
if(_pHead) {
L1Item<T>* p = _pHead;
_pHead = p->pNext;
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(_pHead) {
if(_pHead->pNext) {
L1Item<T>* prev = _pHead;
L1Item<T>* pcur = prev->pNext;
while(pcur->pNext) {
prev = pcur;
pcur = pcur->pNext;
}
delete pcur;
prev->pNext = NULL;
}
else {
delete _pHead;
_pHead = NULL;
}
_size--;
return 0;
}
return -1;
}
#endif //A01_LISTLIB_H