forked from raolokesh126/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
92 lines (88 loc) · 1.32 KB
/
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
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
#include<bits/stdc++.h>
using namespace std;
class Stack{
//properties
public:
int top;
int size;
int *arr;
//behaviour
//making constructor
Stack()
{
top=-1;
size=5;
arr=new int[size];
}
void Push(int x)
{
if(top+1>=size)
{
cout<<"stack overflow for: "<< x <<endl;
}
else
{
top++;
arr[top]=x;
}
}
int Top()
{
if(top==-1)
{
cout<<"stack under flow"<<endl;
return -1;
}
else
return arr[top];
}
void pop()
{
if(top==-1)
cout<<"stack is underflow"<<endl;
else
top=top-1;
}
int Size()
{
return top+1;
}
bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}
void print()
{
if(top==-1)
cout<<"stack is empty!"<<endl;
else //print the stack
{
int tmp=top;
while(tmp!=-1)
{
cout<<arr[tmp--]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Stack s;
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(1);
s.Push(5);
s.Push(6);
s.pop();
cout<<"stack is: ";
s.print();
cout<<"top element of the stack: "<<s.Top()<<endl;
cout<<"stack size is: "<<s.Size()<<endl;
cout<<"is stack empty: "<<s.isEmpty()<<endl;
return 0;
}