-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackByArray.cpp
More file actions
63 lines (53 loc) · 957 Bytes
/
StackByArray.cpp
File metadata and controls
63 lines (53 loc) · 957 Bytes
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
#include <iostream>
using namespace std;
const int MAX=100;
class Stack{
private:
int a[MAX];
int topIndex;//index on the top
bool full() const;
public:
Stack();
bool empty() const;
void push(int const& x);//pushing an element
int pop();//removing an element
int top() const; //return the top of the stack
};
Stack::Stack():topIndex(-1){}
bool Stack::full() const
{
return topIndex==MAX-1;
}
bool Stack::empty() const
{
return topIndex==-1;
}
void Stack::push(int const& x){
if(full())
{
cerr<<"Stack is full\n";
}else
a[++topIndex]=x;
}
int Stack::pop()
{
if(empty())
{
cerr<<"Empty stack!\n";
return 0;
}
return a[topIndex--];
}
int Stack::top() const
{
if(empty())
{
cerr<<"empty stack\n";
return 0;
}
return a[topIndex];
}
int main()
{
return 0;
}