-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkStack.cpp
51 lines (51 loc) · 1.11 KB
/
linkStack.cpp
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
#include<iostream>
#include"error.cpp"
template<class T>
class linkStack
{
private:
struct Node
{
T data;
Node*next;
Node():next(NULL){}
Node(const T &x,Node *p):data(x),next(p){}
};
Node *top;
public:
linkStack():top(NULL){}
~linkStack()
{
Node *tmp;
while(top)
{
tmp=top;
top=top->next;
delete tmp;
}
}
void push(const T &x)
{
Node *tmp=new Node(x,top);
top=tmp;
}
T pop()
{
if(top)
{
Node*tmp=top;
T x=top->data;
top=top->next;
delete tmp;
return x;
}
throw error("stack pop error");
}
bool isEmpty(){return top==NULL;}
T Top()
{
if(!top)
throw error("stack top error");
return top->data;
}
};