-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
92 lines (89 loc) · 1.2 KB
/
stack.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
//implimentation of stack using an array.
#include<stdio.h>
#define N 5
void push();
void pop();
void peek();
void display();
int main()
{
int ch;
do
{
printf("\nEnter choice: 1.Push 2.Pop 3.Peek 4.Display");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
printf("Enter Invalid Choice");
}
}while(ch != 0);
}
void push()
{
int x;
int top = -1;
int stack[N];
printf("Enter an element: ");
scanf("%d", &x);
if(top == N-1)
{
printf("Stack Overflow....");
}
else
{
top++;
stack [top] = x;
}
}
void pop()
{
int item;
int stack[N];
int top;
if(top == -1)
{
printf("Stack underflow....");
}
else
{
item = stack[top];
top--;
printf("%d", item);
}
}
void peek()
{
int top;
int stack[N];
if(top == -1)
{
printf("Stack is_empty..");
}
else
{
printf("%d", stack[top]);
}
}
void display()
{
int i;
int stack[N];
int top;
for(i = top; i >= 0; i--)
{
printf("%d",stack[i]);
}
}