forked from bhonesh1998/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_operations_on_stack.c
More file actions
118 lines (92 loc) · 2.74 KB
/
common_operations_on_stack.c
File metadata and controls
118 lines (92 loc) · 2.74 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
***********************************************************
***********************************************************
NAME-BHONESH CHAWLA
REGNO-20164017
BATCH-CS-1
CONTACT-+918619127663
EMAIL-rajachawla778@gmail.com
***********************************************************
***********************************************************
*/
#include<stdio.h>
#include<stdlib.h>
#define pf printf
#define sf scanf
#define max 100
int stack[max];
int top=-1;
//---------------------------------------------------------------------------------------------------------------------
void push(int x){
if(top==max-1){
pf("Stack overflow\n");
return;}
else if(top==-1){
top=0;
stack[top]=x;}
else
{ top++;
stack[top]=x;
}
pf("New stack is : ");
for(int i=0;i<=top;i++)
pf("%d ",stack[i]);
pf("\n");
}
//---------------------------------------------------------------------------------------------------------------------
int pop(){
if(top==-1){
pf("Stack is underflow\n");
return 0;
}
else{
int item=stack[top];
top--;
return item;}
}
//---------------------------------------------------------------------------------------------------------------------
int middle(){
int middle=(top+1)/2;
int item=stack[middle];
return item;
}
//---------------------------------------------------------------------------------------------------------------------
void deletemiddle(){
int middle=(top+1)/2;
for(int i=middle;i<top;i++){
int tem=stack[i];
stack[i]=stack[i+1];
stack[i+1]=tem;
}
pf("New stack is : ");
for(int i=0;i<top;i++)
pf("%d ",stack[i]);
pf("\n");
}
//---------------------------------------------------------------------------------------------------------------------
int main(){
int a,item;
while(1){
pf("1. Push element in stack\n2. Pop element from stack\n3. find middle of stack\n4.Delete middle element of stack\n5.exit from loop\nPlz eneter ur choice\n");
sf("%d",&a);
switch(a){
case 1: pf("Enter the item\n");
sf("%d",&item);
push(item);
break;
case 2: item = pop();
pf("Poped item is = %d\n",item);
break;
case 3: item = middle();
pf("Middle item is = %d\n",item);
break;
case 4: deletemiddle();
break;
case 5: exit(1);
break;
default : pf("wrong choice\n");
}
}
return 0;
}
//---------------------------------------------------------------------------------------------------------------------