-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.h
More file actions
55 lines (54 loc) · 1.05 KB
/
Stack.h
File metadata and controls
55 lines (54 loc) · 1.05 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
#include<iostream>
using namespace std;
#ifndef STACK
#define STACK
const int STACK_CAPACITY=128;
typedef int StackElement;
class Stack
{
public:
Stack();
bool empty() const;
void push(const StackElement &value);
void display(ostream &out) const;
StackElement top() const;
void pop();
private:
StackElement myArray[STACK_CAPACITY];
int myTop;
};
inline Stack::Stack()
{ myTop=-1;}
inline bool Stack::empty() const
{ return (myTop==-1);}
void Stack::push(const StackElement &value)
{
if(myTop<STACK_CAPACITY-1)
{
++myTop;
myArray[myTop]=value;
}
else
cerr<<"*** Stack is full---you can't add new value***\n"
<<"Must increase value of STACK_CAPACITY in Stack.h\n";
}
void Stack::display(ostream &out) const
{
for(int i=myTop;i>=0;i--)
cout<<myArray[i]<<endl;
}
StackElement Stack::top() const
{
if(myTop>=0)
return myArray[myTop];
cerr<<"***Stack is empty***\n";
return 0;
}
void Stack::pop()
{
if(myTop>=0)
myTop--;
else
cerr<<"***Stack is empty-can't remove a value***\n";
}
#endif