-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack1.c
91 lines (91 loc) · 2.07 KB
/
stack1.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
#include<stdio.h>
#include<stdlib.h>
struct ArrayStack
{
int top;
int capacity;
int *array;
};
struct ArrayStack *createStack(int cap)
{
struct ArrayStack *stack;
stack=(struct ArrayStack*)malloc(sizeof(struct ArrayStack));
stack->capacity=cap;
stack->top=-1;
stack->array=(int *)malloc(sizeof(int)*stack->capacity);
return(stack);
};
int full(struct ArrayStack *stack)
{
if(stack->top==(stack->capacity)-1)
return 1;
else
return 0;
}
int empty(struct ArrayStack *stack)
{
if(stack->top==-1)
return 1;
else
return 0;
}
void push(struct ArrayStack *stack,int item)
{
if(!full(stack))
{
stack->top++;
stack->array[stack->top]=item;
}
else
printf("\nsry..stack is full..last value can't be pushed..now can't pushed any more\n");
}
int pop(struct ArrayStack *stack)
{
int item;
if(!empty(stack))
{
item=stack->array[stack->top];
stack->top--;
return item;
}
else
return(-1);
}
int main()
{
int item,choice,i;
struct ArrayStack *stack;
stack=createStack(3);
printf("\n1. push");
printf("\n2. pop");
printf("\n3. display");
printf("\n4. exit");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the no. to be pushed : ");
scanf("%d",&item);
push(stack,item);
break;
case 2:
item=pop(stack);
if(item==-1)
printf("\nstack is empty..");
else
printf("\nthe popped value is %d",item);
break;
case 3:
printf("the stack is ");
for(i=0;i<=stack->top;i++)
printf("%d ",stack->array[i]);
break;
case 4:
exit(0);
}
}
return 0;
}