-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.cpp
54 lines (44 loc) · 990 Bytes
/
stack.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
#include<bits/stdc++.h>
using namespace std;
// A structure to represent a stack
int top =-1;
void push (int Stack[ ] , int x , int n) {
if ( top == n-1 ) {
cout << "OVERFLOW" ;
}
else{
top = top +1 ;
Stack[ top ] = x ;
}
}
bool isEmpty ( ){
if ( top == -1 ) //Stack is empty
return true ;
else
return false;
}
void pop (int Stack[ ] ,int n ){
if( isEmpty ( ) )
{
cout << "Underflow" << endl ;
}
else
{
top = top - 1 ;
}
}
int topElement ( int Stack[]){
return Stack[top];
}
int main(){
int s[5];
cout<<isEmpty()<<endl; //returns 1
push(s, 4, 5);
push(s, 5, 5);
push(s, 7, 5);
push(s, 3, 5);
push(s, 2, 5); // s[] = {4, 5, 7, 3, 2}
cout<<topElement(s); //2
pop(s, 5); //s[] = {4, 5, 7, 3}
cout<<topElement(s); //3
}