-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.c
More file actions
77 lines (65 loc) · 1.43 KB
/
Copy path15.c
File metadata and controls
77 lines (65 loc) · 1.43 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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 8
struct Stack {
int items[MAX_SIZE];
int top;
};
void initialize(struct Stack *stack) {
stack->top = -1;
}
int isEmpty(struct Stack *stack) {
return stack->top == -1;
}
int isFull(struct Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
void push(struct Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack is full. Cannot push %d.\n", value);
return;
}
stack->items[++stack->top] = value;
printf("Pushed %d onto the stack.\n", value);
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty. Cannot pop.\n");
return -1;
}
int popped = stack->items[stack->top--];
return popped;
}
int peek(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return -1;
}
return stack->items[stack->top];
}
void display(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = 0; i <= stack->top; ++i) {
printf("%d ", stack->items[i]);
}
printf("\n");
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 6);
push(&stack, 7);
push(&stack, 8);
push(&stack, 5);
push(&stack, 3);
pop(&stack);
push(&stack, 10);
pop(&stack);
pop(&stack);
display(&stack);
return 0;
}