-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackOperations_Array.c
93 lines (75 loc) · 1.26 KB
/
StackOperations_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
//stack full check
int stack_full(int *top,int n){
if (*top ==n){
return 1;
}
else{
return 0;
}
}
//stack empty check
int stack_empty(int *top){
if (*top ==0){
return 1;
}
else{
return 0;
}
}
//push operations
void push(int *s, int data, int *top, int n){
if (stack_full(top,n)==1){
printf("\nStack Full");
}
else{
s[*top]=data;
(*top)++;
}
}
//pop operations
int pop(int *s, int *top){
int data;
if (stack_empty(top)==1){
printf("\nEmpty Stack");
return ' ';
}
else{
(*top)--;
data=s[*top];
printf("Data popped is %d",data);
return data;
}
}
void main(){
int s[100];
int n=100;
//initialization of a top pointer
int *top=(int*)malloc(sizeof(int));
*top=0;
int status=0;
while (status==0){
printf("\n1:Push Operations\n2:Pop Operations");
int ch;
printf("\nEnter your choice");
scanf("%d",&ch);
if (ch==1){
printf("\nEnter the element to push");
int ele;
scanf("%d",&ele);
push(s,ele,top,n);
}
else if (ch==2){
int popped_ele=pop(s,top);
if (popped_ele != ' '){
printf("\n%d was popped",popped_ele);
}
}
else{
printf("Invalid Choice, try again");
}
printf("\nEnter 0 to continue stack operations");
scanf("%d",&status);
}
}