-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackByArray.c
More file actions
80 lines (71 loc) · 1.68 KB
/
StackByArray.c
File metadata and controls
80 lines (71 loc) · 1.68 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
#include<stdio.h>
#include<stdlib.h>
typedef struct ArrayStack{
int capacity;
int *stack;
int top;
} arrstack;
void createstack(arrstack* arrs,int capacity){
arrs->capacity=capacity;
arrs->stack=(int*)malloc(capacity * sizeof(int));
arrs->top=-1;
printf("Array is Created\n");
}
void push(arrstack* stk, int element){
if(stk->top==stk->capacity){
printf("Stack is Full");
}else{
stk->top ++;
*(stk->stack + stk->top)=element;
}
}
int pop(struct ArrayStack* stk){
if(stk->top == -1){
printf("Underflow");
}else{
int temp= *(stk->stack + (stk->top));
stk->top--;
return temp;
}
}
void display(struct ArrayStack* stk){
if(stk->top == -1){
printf("Underflow");
}else{
int i=stk->top;
while(i >=0){
printf("%d ",*(stk->stack +i));
i--;
}
printf("\n");
}
}
int main(){
arrstack stack;
int choice=0,element;
createstack(&stack,8);
while(choice !=4 ){
printf("Press 1 to push element to stack\n");
printf("Press 2 to pop from stack\n");
printf("Press 3 to display\n");
printf("Press 4 to exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("Enter value to push\n");
scanf("%d",&element);
push(&stack,element);
break;
case 2:
printf("popped Element is : %d\n",pop(&stack));
break;
case 3:
display(&stack);
break;
case 4:
exit(0);
}
}
return 0;
}