-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStack.h
More file actions
75 lines (71 loc) · 1.23 KB
/
CStack.h
File metadata and controls
75 lines (71 loc) · 1.23 KB
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
#ifndef CSTACK_H
#define CSTACK_H
#include <iostream>
template <typename T>
struct SNode {
T Data;
SNode<T>* Next;
SNode(T data,SNode<T> *next) {
Data = data;
Next = next;
}
};
template <typename T>
class Stack {
int Size;
SNode<T> *Head;//Ö¸ÏòÕ»¶¥
public:
Stack<T>() : Size(0), Head(NULL) {}
Stack<T>(Stack<T> &S) {
Size = S.Size;
if (Size == 0)
Head = NULL;
else {
Head = new SNode<T>(Head->Data, NULL);
SNode<T> *Tp = Head;
SNode<T> *Sp = S.Head->Next;
while (Sp != NULL) {
SNode<T> *temp = new SNode<T>(Sp->Data, NULL);
Tp->Next = temp;
Sp = Sp->Next;
}
}
}
void Push(T x);
T Pop();
bool IsEmpty();
T GetTop();
int GetLength();
};
template <typename T>
void Stack<T>::Push(T x) {
SNode<T> *newP = new SNode<T>(x, Head);
Head = newP;
Size++;
}
template <typename T>
T Stack<T>::Pop() {
if (Head == NULL) {
std::cout << "TheStackIsEmpty" << std::endl;
exit(1);
}
T temp = Head->Data;
SNode<T>* P = Head;
Head = Head->Next;
delete P;
Size--;
return temp;
}
template <typename T>
bool Stack<T>::IsEmpty() {
return Head == NULL ? true : false;
}
template <typename T>
T Stack<T>::GetTop() {
return Head->Data;
}
template <typename T>
int Stack<T>::GetLength() {
return Size;
}
#endif