-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26_stack_operations.cpp
86 lines (82 loc) · 2.07 KB
/
26_stack_operations.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
// Coding Push() , Pop() , isEmpty() and isFull() Operations in Stack using an Array
#include <iostream>
using namespace std;
class Stack
{
public:
int size;
int top;
int *arr;
Stack(int tsize, int ttop)
{
size = tsize;
top = ttop;
arr = new int[tsize];
}
int isEmpty(Stack *ptr)
{
if (ptr->top == -1)
{
return 1;
}
else
{
return 0;
}
}
int isFull(Stack *ptr)
{
if (ptr->top == size - 1)
{
return 1;
}
return 0;
}
void push(Stack* ptr , int val){
if(isFull(ptr)){
cout<<"Stack Overflow and can not push "<<val<<endl;
}
else{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}
int pop(Stack* ptr){
if(isEmpty(ptr)){
cout<<"Stack Underflow and can not pop from the stack . "<<endl;
return -1;
}
else{
int val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
}
};
int main()
{
Stack *sp = new Stack(10, -1);
cout << "Stack has been created successfully . " << endl;
cout<<"Before Pushing . "<<endl;
cout<<"Is FULL : "<<sp->isFull(sp)<<endl;
cout<<"Is EMPTY : "<<sp->isEmpty(sp)<<endl;
sp->push(sp , 1);
sp->push(sp , 23);
sp->push(sp , 99);
sp->push(sp , 75);
sp->push(sp , 3);
sp->push(sp , 64);
sp->push(sp , 57);
sp->push(sp , 46);
sp->push(sp , 89);
sp->push(sp , 6); // ---> Pushed 10 values .
sp->push(sp , 46); // ---> Stack Overflow since the size of the Stack is 10 .
// sp->push(sp , 90);
cout<<"After pushing . "<<endl;
cout<<"Is FULL : "<<sp->isFull(sp)<<endl;
cout<<"Is EMPTY : "<<sp->isEmpty(sp)<<endl;
cout<<"Popped "<<sp->pop(sp)<<" from the Stack . "<<endl; // Last in First out
cout<<"Popped "<<sp->pop(sp)<<" from the Stack . "<<endl; // Last in First out
cout<<"Popped "<<sp->pop(sp)<<" from the Stack . "<<endl; // Last in First out
return 0;
}