-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
91 lines (75 loc) · 1.71 KB
/
Stack.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
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
#include "Stack.h"
#include "Move.h"
#include <stdexcept>
#include <iostream>
using namespace std;
template <typename T>
Stack<T>::Stack(int size) : capacity(size), top(-1) {
arr = new T[capacity];
}
template <typename T>
Stack<T>::Stack(const Stack& other) : capacity(other.capacity), top(other.top) {
arr = new T[capacity];
for (int i = 0; i <= top; i++) {
arr[i] = other.arr[i];
}
}
template <typename T>
Stack<T>& Stack<T>::operator=(const Stack& other) {
if (this == &other) {
return *this;
}
delete[] arr;
capacity = other.capacity;
top = other.top;
arr = new T[capacity];
for (int i = 0; i <= top; ++i) {
arr[i] = other.arr[i];
}
return *this;
}
template <typename T>
Stack<T>::~Stack() {
delete[] arr;
}
template <typename T>
void Stack<T>::push(T element) {
if(isFull()) { throw overflow_error("Stack is full");}
top++;
arr[top] = element;
}
template <typename T>
T Stack<T>::pop() {
if(isEmpty()) { throw underflow_error("Stack is ");}
else { return arr[top--]; }
}
template <typename T>
T Stack<T>::peek() const {
if(isEmpty()) {throw out_of_range("Stack is empty");}
else return arr[top];
}
template <typename T>
bool Stack<T>::isEmpty() const {
if (top == -1)
return true;
else return false;
}
template <typename T>
bool Stack<T>::isFull() const {
if(top==capacity-1)
{
return true;
}
else return false;
}
template <typename T>
void Stack<T>::display() const {
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
template class Stack<int>;
template class Stack<char>;
template class Stack<double>;
template class Stack<Move>;