-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.hpp
More file actions
96 lines (83 loc) · 1.71 KB
/
stack.hpp
File metadata and controls
96 lines (83 loc) · 1.71 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
#include <stdexept>
#include <new>
#include <cstdlib>
namespace AFBIC{
template<typename T>
class Stack{
private:
T* inptr;
int size,toploc;
//toploc:highest empty location
public:
explicit Stack(const int s);
explicit Stack(const Stack s);
explicit Stack(const Stack&& s);
~Stack();
void operator=(Stack s);
void push(T data);
void pop();
int size();
T top();
bool empty();
}//Stack
}//namespace AFBIC
AFBIC::Stack::Stack(const int s){
try{
inptr=new T[s];
}
catch(std::bad_alloc& ba){
size=0;
toploc=0;
inptr=nullptr;
throw;
}
size=s;
toploc=0;
}
AFBIC::Stack::Stack(const Stack s){
try{
inptr=new T[s.size];
}
catch(std::bad_alloc& ba){
size=0;
toploc=0;
inptr=nullptr;
throw;
}
size=s.size;
toploc=s.toploc;
for(int head=0;head!=toploc;head++){
inptr[head]=s.inptr[head];
}
}
AFBIC::Stack::Stack(const Stack&& s){
inptr=s.inptr; //just moving
size=s.size;
toploc=s.toploc;
}
AFBIC::Stack::~Stack(){
delete(inptr);
size=0;
toploc=0;
}
bool AFBIC::Stack::empty(){
if(toploc==0) return true;
return false;
}
int AFBIC::Stack::size(){
return toploc;
}
template <typename T>
T AFBIC::Stack::top(){
return inptr[toploc-1];
}
void AFBIC::Stack::pop(){
if(empty()) throw std::runtime_error("Stack Underflow");
else toploc=toploc-1;
}
template <typename T>
void AFBIC::Stack::push(T data){
if(toploc>size) throw std::runtime_error("Stack Overflow");
inptr[toploc]=data;
toploc++;
}