-
Notifications
You must be signed in to change notification settings - Fork 484
/
StacksWithoutSTL.cpp
99 lines (81 loc) Β· 1.45 KB
/
StacksWithoutSTL.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
92
93
94
95
96
97
98
99
//Implementation of Stack with its operations.
#include <iostream>
using namespace std;
#define MAX 5
//Declaration of Stack
class Stack
{
private:
int top;
int ele[MAX];
public:
Stack();
bool isFull();
bool isEmpty();
int peek();
void push(int item);
int pop(int *item);
};
//Initialization of stack
Stack:: Stack()
{
top = -1;
}
//Check stack is full or not
bool Stack:: isFull()
{
return top==MAX-1;
}
//Check stack is empty or not
bool Stack:: isEmpty()
{
return top==-1;
}
//Return value of item at top of stack
int Stack:: peek()
{
if (!isEmpty()) {
return arr[top];
}
else {
exit(EXIT_FAILURE);
}
}
//Insert item into stack
void Stack:: push( int item )
{
if( isFull() )
{
cout<<"\nStack Overflow";
return;
}
top++;
ele[top] = item;
cout<<"\nInserted item : "<< item;
}
//Delete item from stack
int Stack:: pop( int *item )
{
if( isEmpty() )
{
cout<<"\nStack Underflow";
return -1;
}
*item = ele[top--];
return 0;
}
int main()
{
int item = 0;
Stack s = Stack();
for(int i=1;i<=MAX;i++)
s.push(i*10);
// s can store max of 5 elements.
// Pushing one more element will result into overflow situation.
s.push(60);
while( s.pop(&item)== 0 ){
cout<<"\nDeleted item : "<< item;
}
cout<< endl;
return 0;
}