-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcStack.hpp
More file actions
127 lines (109 loc) · 2.34 KB
/
cStack.hpp
File metadata and controls
127 lines (109 loc) · 2.34 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
#ifndef _CSTACK_H
#define _CSTACK_H
#include <iostream>
using namespace std;
template <typename E>
class cStack {
private:
int cap;
E* ar;
int top_r;
int top_b;
public:
cStack(int cap = 100); // constructor from capacity
cStack(const cStack<E> &S); //copy constructor
int redsize() const; // number of items in stack
bool redempty() const; // is the stack empty?
const E& redtop() const; // the top element
void redpush(const E& e); // push x onto the stack
void redpop(); // remove the top element
int bluesize() const; // number of items in stack
bool blueempty() const; // is the stack empty?
const E& bluetop() const; // the top element
void bluepush(const E& e); // push x onto the stack
void bluepop(); // remove the top element
};
template <typename E>
cStack<E>::cStack(int n){
this->cap = n;
ar = new E[cap];
top_r = -1;
top_b = n;
}
template <typename E>
cStack<E>::cStack(const cStack<E> &S){
cap = S.cap;
ar = new E[cap];
for(int i=0;i<cap;i++){
ar[i]=S.ar[i];
}
top_r = S.top_r;
top_b = S.top_b;
}
template <typename E>
int cStack<E>::redsize() const{
return top_r+1 ;
}
template <typename E>
int cStack<E>::bluesize() const{
return cap-top_b;
}
template <typename E>
bool cStack<E>::redempty() const{
if(top_r==-1)return true;
return false;
}
template <typename E>
bool cStack<E>::blueempty() const{
if(top_b==cap)return true;
return false;
}
template <typename E>
void cStack<E>::redpush(const E& e){
if(top_b-top_r > 1){
top_r++;
ar[top_r] = e;
}
else{
cout << "ERROR: Full" << endl;
return;
}
}
template <typename E>
void cStack<E>::bluepush(const E& e){
if(top_b-top_r > 1){
top_b--;
ar[top_b] = e;
}
else{
cout << "ERROR: Full" << endl;
return;
}
}
template <typename E>
const E& cStack<E>::redtop() const{
if(!redempty())return ar[top_r];
else cout << "ERROR: Empty" << endl;
}
template <typename E>
const E& cStack<E>::bluetop() const{
if(!blueempty())return ar[top_b];
else cout << "ERROR: Empty" << endl;
}
template <typename E>
void cStack<E>::redpop(){
if(!redempty())top_r--;
else{
cout << "ERROR: Red Empty" << endl;
return;
}
}
template <typename E>
void cStack<E>::bluepop(){
if(!blueempty())top_b++;
else{
cout << "ERROR: Blue Empty" << endl;
return;
}
}
#endif