-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
82 lines (75 loc) · 1.63 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
#include <stdio.h>
#define MAXSIZE 10
void push(int[], int *, int);
int pop(int[], int *);
void print_stack(int[], int);
int main()
{
int stack[MAXSIZE], tos = -1, value, choice;
while (1)
{
printf("\n1. Push\n2. Pop\n3. Print Stack\n4. Stack Calculation\n 5.Exit\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(stack, &tos, value);
break;
case 2:
value = pop(stack, &tos);
if (tos != -1) // check if the stack was not empty
{
printf("Popped value: %d\n", value);
}
break;
case 3:
print_stack(stack, tos);
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
}
void push(int stack[], int *tos, int value)
{
if (*tos == MAXSIZE - 1)
{
printf("Stack is full\n");
}
else
{
stack[++(*tos)] = value;
printf("Pushed value: %d\n", value);
}
}
int pop(int stack[], int *tos)
{
if (*tos == -1)
{
printf("The stack is empty\n");
return -1; // Return a sentinel value to indicate an error
}
else
{
return stack[(*tos)--];
}
}
void print_stack(int stack[], int tos)
{
if (tos == -1)
{
printf("The stack is empty\n");
}
else
{
printf("Stack contents: ");
for (int i = 0; i <= tos; i++)
{
printf("%d ", stack[i]);
}
printf("\n");
}
}