-
Notifications
You must be signed in to change notification settings - Fork 4
/
stack1.cpp
63 lines (53 loc) · 1004 Bytes
/
stack1.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
#include <iostream>
#define size 5
using namespace std;
int stack[size], top=-1;
//to insert element in the stack
int push (int element)
{
if(top==size-1) cout<<"stack is full element cannot be insert";
else
{
top++;
stack[top] = element;
cout<<"your element is "<<stack[top]<<"pushed"<<endl;
}
}
//to delete element from stack
void pop (void){
if(top==-1) cout<<"stack is empty cannot delete any element\n";
else
{
cout<<"your top element" <<stack[top]<<" is popped"<<endl;
top--;
}
}
//to display element of stack
int display (void)
{
int i;
cout<<"Display ->";
if(top==-1) cout<<"stack is empty insert any element to display"<<endl;
else for(i=0; i<top; i++)
{
cout<<stack[i]<<"\t";
}
}
//to peek top element in stack
int peek (void)
{
if(top==-1) cout<<"stack is empty!!";
else
{
cout<<stack[top];
}
}
int main (){
push(10);
push(20);
push(30);
pop();
display();
peek();
return 0;
}