-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStack.h
144 lines (126 loc) · 2.88 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
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
/**
* @file Stack.h
*
* @author <a href="mailto:[email protected]">Amirhossein Hakimnejad</a>
* @date Feb 19, 2018
* @date Mar 6, 2018
*/
#ifndef BIGNUMBER_STACK_H
#define BIGNUMBER_STACK_H
#include <iostream>
using std::istream;
using std::ostream;
using std::endl;
template <typename T> class Stack;
template <typename T> istream& operator>>(istream &, Stack<T> &);
template <typename T> ostream& operator<<(ostream &, const Stack<T> &);
template <typename T>
class Stack{
friend class BigInt;
friend istream& operator>> <T>(istream &, Stack<T> &);
friend ostream& operator<< <T>(ostream &, const Stack<T> &);
private:
T *ptr;
int size;
int top;
public:
explicit Stack(int=10);
~Stack();
int getSize() const;
int getTop() const;
bool push(T);
bool pop();
bool isEmpty() const;
bool isFull() const;
T &operator [] (int);
T operator [] (int)const;
const Stack& operator = (const Stack&);
const Stack& operator += (const Stack &);
};
template <typename T>
Stack<T>::Stack(int n){
size = n > 0 ? n : 10;
top = -1;
ptr = 0;
ptr = new T[size];
for(int i = 0; i < size; i++)
ptr[i] = 0;
}
template <typename T>
Stack<T>::~Stack(){
delete[] ptr;
ptr = 0;
}
template <typename T>
bool Stack<T>::push(T val){
if(!isFull()){
ptr[++top] = val;
return true;
}
return false;
}
template <typename T>
bool Stack<T>::pop(){
if (!isEmpty()){
top--;
return true;
}
return false;
}
template <typename T>
int Stack<T>::getSize() const{
return size;
}
template <typename T>
int Stack<T>::getTop() const{
return top;
}
template <typename T>
bool Stack<T>::isEmpty()const{
return getTop() == -1;
}
template <typename T>
bool Stack<T>::isFull()const{
return getTop() == getSize() - 1;
}
template <typename T>
T &Stack<T>::operator [] (int n) {
if(n < 0 || n >= getSize()) {
throw(std::out_of_range("Out of range index in stack::operator []."));
}
return ptr[n];
}
template <typename T>
T Stack<T>::operator [] (int n) const{
if(n < 0 || n >= getSize()) {
throw(std::out_of_range("Out of range index in stack::operator []."));
}
return ptr[n];
}
template <typename T>
const Stack<T>& Stack<T>::operator = (const Stack&r){
if(getSize() != r.getSize()){
delete[] ptr;
size = r.getSize();
ptr = new T[size];
}
for(int i = 0; i < getSize(); i++)
ptr[i] = r[i];
return *this;
}
template <typename T>
istream &operator>> (istream &input, Stack<T> &a){
for(int i = 0; i < a.getSize(); i++) {
input >> a.ptr[i];
a.top++;
}
return input;
}
template <typename T>
ostream& operator<< (ostream &output, const Stack<T> &a){
for(int i = 0; i < a.getSize(); i++) {
output << a.ptr[i];
}
return output << endl;
}
#endif //BIGNUMBER_STACK_H