-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.h
More file actions
154 lines (104 loc) · 2.65 KB
/
stack.h
File metadata and controls
154 lines (104 loc) · 2.65 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef STACK
#define STACK
#include "linklist.h"
template <class T>
class Stack{
public:
Stack();
//Big Three
//Copy Constructor
Stack(const Stack<T>& s);
//Destructor
~Stack();
//Assigment Operator
Stack& operator =(const Stack& list);
void push(T item);
//Precondition: item can be any type.
//Postcondition: Inserts a new element at the top of the stack,
//above its current top element.
T pop();
//Precondition:The stack needs to have an element
//Postcondition:Removes the element on top of the stack,
//effectively reducing its size by one.
//The element removed is the latest element
//inserted into the stack.Return the elements on top of the stack
T &top();
//Precondition:The stack needs to have an element
//Postcondition:Since stacks are last-in first-out containers,the top element is the
//last element inserted into the stac
bool empty();
//Precondition: container contains 0 or more elements
//postcondition:Returns whether the stack is empty
int size()const;
//Return the numbers of elements in the stack
template <class U>
friend ostream& operator<<(ostream& outs,const Stack<U>& RHS);
private:
Node<T>* _stack;
int _size;
};
#endif // Stack
template<class T>
Stack<T>::Stack():_stack(NULL),_size(0){}
//Big Three
//Copy constructor
template<class T>
Stack<T>::Stack(const Stack<T> &s){
_stack=NULL;
_CopyList(s._stack,_stack);
_size=s.size();
}
//Destructor
template<class T>
Stack<T>::~Stack(){
_ClearAll(_stack);
}
//Assigment Operator
template<class T>
Stack<T>&Stack<T>::operator=(const Stack<T>& list){
if (this!=&list){
_ClearAll(_stack);
_stack=NULL;
_CopyList(list._stack,_stack);
_size=getSize(list._stack());
}
return *this;
}
template<class T>
void Stack<T>::push(T item){
_InsertHead(_stack,item);
_size++;
}
template<class T>
T Stack<T>::pop(){
if(!empty()){
_size--;
return _RemoveHead(_stack);
}
throw 0;
}
template<class T>
T &Stack<T>::top(){
if (!empty())
return _stack->item;
throw 0;
}
template<class T>
bool Stack<T>::empty(){
if (_size==0)
return true;
if (_size>0)
return false;
throw 0;
}
template<class T>
int Stack<T>::size()const{
if (_size>=0)
return _size;
throw 0;
}
template <class U>
ostream& operator<<(ostream& outs,const Stack<U>& RHS){
outs<<RHS._stack;
return outs;
}