-
Notifications
You must be signed in to change notification settings - Fork 18
/
Stack_using_array.c
73 lines (68 loc) · 1.41 KB
/
Stack_using_array.c
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
#include<stdio.h>
#define MAXSIZE 100
struct stack{
int stack[MAXSIZE];
int top;
};
struct stack* s;
void push(){
//check if stack is full
int data;
if(s->top==MAXSIZE-1){
printf("Stack Overflow!");
}
else{
printf("Enter data: ");
scanf("%d",&data);
s->stack[++s->top]=data;
}
}
void pop(){
//check if stack is empty
int data;
if(s->top==-1){
printf("Stack Undeflow!");
}
else{
data=s->stack[s->top];
s->top--;
printf("The deleted element is:%d\n",data);
}
}
void traverse(){
//check if stack is empty
if(s->top==-1){
printf("Stack is Empty!");
}
else{
for(int i=0;i<=s->top;i++){
printf("%d ",s->stack[i]);
}
printf("\n");
}
}
int main(){
int user_choice;//,data;
char choice;
s->top=-1;
do{
printf("1)Push\n2)pop\n3)traverse\nEnter your choice: ");
scanf("%d",&user_choice);
switch(user_choice){
case 1:
push();
break;
case 2:;
pop();
break;
case 3:
traverse();
break;
default:
printf("Wrong input!\n");
}
printf("Do you want to continue(y/Y): ");
scanf("%c",&choice);
}while(choice=='y' || choice=='Y');
return 0;
}