-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStack.h
94 lines (83 loc) · 1.36 KB
/
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
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
#include"Node.h"
#include <cassert>
template <class T>
class Stack
{
Node<T>* Head;
void copyStack(const Stack<T>& R) {
if (R.Head == NULL)
{
IntializeStack();
return;
}
if (R.Head == Head)
return;
IntializeStack();
Node<T>* ptr;
Node<T>* Rptr;
Head = new Node<T>;
Head->setitem(R.Head->getitem());
ptr = Head;
Rptr = R.Head->getnext();
while (Rptr) {
ptr->getnext() = new Node<T>;
ptr = ptr->getnext();
ptr->setitem(Rptr->getitem());
Rptr = Rptr->getnext();
}
ptr->setnext(NULL);
}
public:
Stack() {
Head = NULL;
}
Stack(const Stack<T>& R) {
Head = NULL;
copyStack(R);
}
void IntializeStack() {
if (Head == NULL)
return;
Node<T>* ptr = Head;
Node<T>* nextptr;
while (ptr) {
nextptr = ptr->getnext();
delete ptr;
ptr = nextptr;
}
Head = NULL;
}
bool IsEmptyStack() {
return (Head == NULL);
}
const Stack<T>& operator =(const Stack<T>& R) {
copyStack();
return *this;
}
void Push(T data) {
Node <T>* ptr = new Node<T>(data);
if (Head == NULL)
{
Head = ptr;
Head->setnext(NULL);
return;
}
ptr->setnext(Head);
Head = ptr;
}
bool Pop() {
if (IsEmptyStack())
return false;
Node <T>* ptr = Head;
Head = Head->getnext();
delete ptr;
return true;
}
const T& Top() {
assert(Head != NULL);
return Head->getitem();
}
~Stack() {
IntializeStack();
}
};