-
Notifications
You must be signed in to change notification settings - Fork 52
/
array_stack_implementation.c
68 lines (65 loc) · 1.33 KB
/
array_stack_implementation.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
#include<stdio.h>
#include<stdlib.h>
int push(int *array,int *top)
{
int ele;
(*top)=(*top)+1;
if((*top)>=5)
{
printf("Stack overflow\n");
(*top)--;
}
else
{
printf("\nEnter the element to push in stack:\t");
scanf("%d",&ele);
array[(*top)]=ele;
printf("The last element %d is at position %d\n",ele,(*top)+1);
}
return 0;
}
int show(int *array,int *top)
{
if((*top)==-1)
{
printf("no element in the array\n");
}
for(int i=(*top);i>=0;i--)
{
printf("%d\n",array[i]);
}
return 0;
}
int pop(int*array,int *top)
{
if((*top)==-1)
{
printf("Stack underflow!!!\n");
}
else
{
(*top)--;
printf("The element %d is poped from %d position\n",array[(*top)],(*top));
}
return 0;
}
int main()
{
int *array,p=1,choice,size,top=-1;;
printf("Enter the size of stack:\t");
scanf("%d",&size);
array=malloc(size*sizeof(int));
while(p!=0)
{
printf("\n1.Show\n2.Push\n3.Pop\n4.Exit\nEnter the choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:show(array,&top);break;
case 2:push(array,&top);break;
case 3:pop(array,&top);break;
case 4:p=0;break;
}
}
return 0;
}