forked from ejekanshjain/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack using Arrays.c
More file actions
89 lines (89 loc) · 1.53 KB
/
Stack using Arrays.c
File metadata and controls
89 lines (89 loc) · 1.53 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
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<stdlib.h>
#define N 5
typedef struct stack
{
int a[N];
int top;
}stack;
void init(stack *);
void push(stack *);
void pop(stack *);
void peek(stack *);
int main()
{
int c;
stack s;
init(&s);
while(1)
{
printf("1.Push\n2.Pop\n3.Peek\n4.Exit\nEnter a Choice : ");
scanf("%d",&c);
switch(c)
{
case 1:
push(&s);
break;
case 2:
pop(&s);
break;
case 3:
peek(&s);
break;
case 4:
exit(0);
default :
printf("\nEnter a Valid Choice\n");
}
}
return 0;
}
void init(stack *p) //This function is to initialize the array
{
p->top=-1;
}
void push(stack *p)
{
if(p->top==N-1)
{
printf("\nOverFlow\n");
}
else
{
printf("\nEnter Item to be Pushed : ");
int e;
scanf("%d",&e);
p->top++;
p->a[p->top]=e;
}
}
void pop(stack *p)
{
if(p->top==-1)
{
printf("\nUnderFlow\n");
}
else
{
printf("\nDeleted : %d\n",p->a[p->top]);
p->top--;
}
}
void peek(stack *p)
{
if(p->top==-1)
{
printf("\nUnderFlow\n");
printf("Do it again");
}
else
{
int i;
printf("\nStack :\n");
for(i=0;i<=p->top;i++)
{
printf("%d\t",p->a[i]);
}
printf("\n");
}
}