-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchain.h
88 lines (72 loc) · 1.76 KB
/
chain.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
/**
* Assignment 5 for CSE231 Data Structures
*
* 2012. 10. 8
*
* FILE: chain.h
* DESCRIPTION: Template class chain, this class contain
* the declaration and definition of the class
* where is implement chain ADT.
*
* AUTHOR: Vicente Adolfo Bolea Sanchez
* STUDENT NO: 20122901
* EMAIL: [email protected]
*/
#ifndef _CHAIN_
#define _CHAIN_
template <class T>
class chain {
private:
struct node { /* This struct represent the node of the chain */
T elem;
node* link;
node(const T& e, node* l = NULL): elem(e), link(l) {}
};
node *first, *last;
public:
/* Class iterator needed for the dijkstra algorithm */
class iterator {
private:
node* current;
public:
iterator(node* n): current(n) {}
void operator++ (int) {
current = current->link;
}
bool operator== (const iterator& it) const {
return (current == it.current) ? true: false;
}
bool operator!= (const iterator& it) const {
return (current != it.current) ? true: false;
}
const T& getValue() const {
return current->elem;
}
};
chain() {
last = first = NULL;
}
/* Make sure that we delete all the nodes */
~chain() {
for (node* tmp = first; tmp != NULL; ) {
node* aux = tmp;
tmp = tmp->link;
delete aux;
}
}
iterator begin() const { iterator it(first); return it; }
iterator end() const { iterator it(NULL); return it; }
/* Push back the given element in the chain */
void push(const T& elem) {
if (first == NULL && last == NULL)
first = last = new node(elem);
else if (first == last) {
first->link = new node(elem);
last = first->link;
} else {
last->link = new node(elem);
last = last->link;
}
}
};
#endif