forked from patmorin/ods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLList.h
103 lines (88 loc) · 1.26 KB
/
SLList.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
/*
* SLList.h
*
* Created on: 2011-11-25
* Author: morin
* FIXME: This code is completely untested (but was ported from tested Java code)
*/
#ifndef SLLIST_H_
#define SLLIST_H_
#include <stdlib.h>
namespace ods {
template<class T>
class SLList {
T null;
protected:
class Node {
public:
T x;
Node *next;
Node(T x0) {
x = x0;
next = NULL;
}
};
Node *head;
Node *tail;
int n;
public:
SLList() {
null = (T)NULL;
n = 0;
head = tail = NULL;
}
virtual ~SLList() {
Node *u = head;
while (u != NULL) {
Node *w = u;
u = u->next;
delete w;
}
}
int size() {
return n;
}
T peek() {
return head->x;
}
bool add(T x) {
Node *u = new Node(x);
if (n == 0) {
head = u;
} else {
tail->next = u;
}
tail = u;
n++;
return true;
}
T push(T x) {
Node *u = new Node(x);
u->next = head;
head = u;
if (n == 0)
tail = u;
n++;
return x;
}
T remove() {
if (n == 0) return null;
T x = head->x;
Node *u = head;
head = head->next;
delete u;
if (--n == 0) tail = NULL;
return x;
}
T pop() {
if (n == 0) return null;
T x = head->x;
Node *u = head;
head = head->next;
delete u;
if (--n == 0) tail = NULL;
return x;
}
};
} /* namespace ods */
#endif /* SLLIST_H_ */