-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdata_structures_stack.c
More file actions
142 lines (119 loc) · 3.13 KB
/
Copy pathdata_structures_stack.c
File metadata and controls
142 lines (119 loc) · 3.13 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Data Structure: Stack (Array Implementation)
* Author: gpl-gowthamchand
* Date: 2025-01-27
* Description: Complete implementation of stack using arrays with all basic operations
*
* Operations:
* - createStack() - Create new stack
* - push() - Insert element (push onto stack)
* - pop() - Remove element (pop from stack)
* - peek() - View top element without removing
* - isEmpty() - Check if stack is empty
* - isFull() - Check if stack is full
* - display() - Display stack contents
*
* Features:
* - Fixed size array implementation
* - Complete error handling
* - All basic stack operations
*
* Usage: gcc data_structures_stack.c -o stack && ./stack
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// Stack structure
struct Stack {
int data[MAX_SIZE];
int top;
};
// Function to create a new stack
struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
if (stack == NULL) {
printf("Memory allocation failed!\n");
return NULL;
}
stack->top = -1;
return stack;
}
// Function to check if stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
// Function to push element onto stack
void push(struct Stack* stack, int data) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push %d\n", data);
return;
}
stack->data[++stack->top] = data;
printf("Pushed %d onto the stack\n", data);
}
// Function to pop element from stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop from empty stack\n");
return -1;
}
int data = stack->data[stack->top--];
printf("Popped %d from the stack\n", data);
return data;
}
// Function to peek at top element
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
return -1;
}
return stack->data[stack->top];
}
// Function to display stack contents
void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
return;
}
printf("Stack contents (top to bottom): ");
for (int i = stack->top; i >= 0; i--) {
printf("%d ", stack->data[i]);
}
printf("\n");
}
// Function to get stack size
int getSize(struct Stack* stack) {
return stack->top + 1;
}
int main() {
struct Stack* stack = createStack();
if (stack == NULL) {
return 1;
}
printf("=== Stack Operations Demo ===\n");
// Push elements
push(stack, 10);
push(stack, 20);
push(stack, 30);
push(stack, 40);
display(stack);
printf("Stack size: %d\n", getSize(stack));
// Peek at top element
printf("Top element: %d\n", peek(stack));
// Pop elements
pop(stack);
pop(stack);
display(stack);
printf("Stack size: %d\n", getSize(stack));
// Try to pop from empty stack
pop(stack);
pop(stack);
pop(stack);
// Free memory
free(stack);
return 0;
}