-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkStack.h
118 lines (102 loc) · 1.56 KB
/
LinkStack.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
#pragma once
template<class T>
class LinkStack
{
struct Node
{
T data;
Node *next;
} *top;
public:
//构造函数,置空链栈
LinkStack()
{
top = NULL;
}
// 析构函数,释放链栈中各结点的存储空间
~LinkStack()
{
ClearStack();
}
Node* Top() const
{
return top;
}
// 元素x入栈
void Push(T &e);
// 栈顶元素出栈
void Pop(T &e);
// 取栈顶元素
void GetTop(T &e);
// 判断栈是否为空
bool isEmpty()
{
return top == NULL;
}
//清空栈
void ClearStack();
//获取栈元素个数
int length();
LinkStack<T>& operator= (const LinkStack<T> &other);
};
template<class T>
void LinkStack<T>::Push(T &e)
{
Node *s = new Node;
s->data = e;
s->next = top;
top = s; //新结点链入表首,为栈顶
}
template<class T>
void LinkStack<T>::Pop(T &e)
{
Node *s;
if (top == NULL)
return;
e = top->data;
s = top;
top = top->next; //栈顶后移
delete s; //删除原栈顶结点
}
template<class T>
void LinkStack<T>::GetTop(T &e)
{
if (top == NULL)
return;
e = top->data;
}
template<class T>
void LinkStack<T>::ClearStack()
{
// 从栈顶开始释放栈链的每一个结点的存储空间
while (top)
{
Node *s = top;
top = top->next;
delete s;
}
}
template<class T>
int LinkStack<T>::length()
{
int len = 0;
auto s = Top();
while (s)
{
++len;
s = s->next;
}
return len;
}
template<class T>
LinkStack<T> & LinkStack<T>::operator=(const LinkStack<T> & other)
{
ClearStack();
auto s = other.Top();
while (s)
{
Push(s->data);
s = s->next;
}
return *this;
}