-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack4.c
100 lines (85 loc) · 2.37 KB
/
stack4.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
92
93
94
95
96
97
98
99
100
//stack using array using double pointer
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Function to push an element onto the stack
void push(int** top, int* stack, int* count, int value) {
if (*count == MAX) {
printf("Stack Overflow\n");
} else {
if (*top == NULL) {
*top = stack; // Initialize top to the base of the stack
} else {
(*top)++;
}
**top = value;
(*count)++;
printf("%d pushed onto the stack\n", value);
}
}
// Function to pop an element from the stack
void pop(int** top, int* stack, int* count) {
if (*count == 0) {
printf("Stack Underflow\n");
} else {
int value = **top;
if (*top == stack) {
*top = NULL; // Reset top to NULL when stack becomes empty
} else {
(*top)--;
}
(*count)--;
printf("Popped element: %d\n", value);
}
}
// Function to peek the top element of the stack
void peek(int* top, int* stack) {
if (top == NULL) {
printf("Stack is empty\n");
} else {
printf("Top element is: %d\n", *top);
}
}
// Function to display the elements of the stack
void display(int* top, int* stack, int count) {
if (count == 0) {
printf("Stack is empty\n");
} else {
printf("Stack elements are: ");
for (int* i = top; i >= stack; i--) {
printf("%d ", *i);
}
printf("\n");
}
}
int main() {
int stack[MAX];
int* top = NULL; // Top pointer initialized to NULL
int count = 0; // Counter to keep track of the number of elements
int choice, value;
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&top, stack, &count, value);
break;
case 2:
pop(&top, stack, &count);
break;
case 3:
display(top, stack, count);
break;
case 4:
peek(top, stack);
break;
case 5:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}