-
Notifications
You must be signed in to change notification settings - Fork 5
/
linked_list_stack.h
61 lines (47 loc) · 1.05 KB
/
linked_list_stack.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
//
// Created by codercat on 19-3-14.
//
#ifndef ALGORITHM_LINKEDLISTSTACK_H
#define ALGORITHM_LINKEDLISTSTACK_H
#include <cstring>
#include <cassert>
template<typename E>
class LinkedListStack {
private:
typedef struct Node {
E element;
Node *next;
} Node;
Node *topNode = NULL;
unsigned int size = 0;
public:
LinkedListStack() {
this->topNode = NULL;
this->size = 0;
}
void push(E element) {
Node *newNode = new Node;
newNode->element = element;
newNode->next = this->topNode;
this->topNode = newNode;
this->size ++;
}
void pop() {
assert(!this->isEmpty());
Node *topNode = this->topNode;
this->topNode = topNode->next;
delete topNode;
this->size --;
}
E top() {
assert(!this->isEmpty());
return this->topNode->element;
}
bool isEmpty() {
return this->getSize() == 0;
}
unsigned int getSize() {
return this->size;
}
};
#endif //ALGORITHM_LINKEDLISTSTACK_H