-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_operation.c
61 lines (51 loc) · 1.3 KB
/
stack_operation.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
#include <stdlib.h>
#include <stdio.h>
#define MAX_VAL 5
void push_element_to_stack (int *stack, int element, int *top)
{
if (*top >= (MAX_VAL -1)) {
printf ("\n Stack overflow \n");
return;
}
(*top) ++;
stack [*top] = element;
printf ("\n Element pushed %d\n", stack [*top]);
return;
}
void pop_element_from_stack (int *stack, int *top)
{
int temp;
if (*top <= 0) {
printf ("\n Stack underflow\n");
return;
}
temp = stack[*top];
(*top)--;
printf ("\n Popped element = %d", temp);
return;
}
void display_stack (int *stack, int top)
{
int i;
printf ("\n Stack display\n");
for (i=0; i<=top; i++)
printf ("\t %d", stack[i]);
printf ("\n Stack display ends\n");
}
void main ()
{
int stack[MAX_VAL];
int top = -1;
pop_element_from_stack (stack, &top);
push_element_to_stack (stack, 1, &top);
push_element_to_stack (stack, 2, &top);
push_element_to_stack (stack, 3, &top);
display_stack (stack, top);
pop_element_from_stack (stack, &top);
display_stack (stack, top);
push_element_to_stack (stack, 4, &top);
push_element_to_stack (stack, 5, &top);
push_element_to_stack (stack, 6, &top);
push_element_to_stack (stack, 6, &top);
display_stack (stack, top);
}