forked from Shubhanshu-1507/Sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks_with_array.c
More file actions
60 lines (49 loc) · 1.26 KB
/
stacks_with_array.c
File metadata and controls
60 lines (49 loc) · 1.26 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
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
// Define a structure for a stack
struct Stack {
int top;
int array[MAX];
};
// Function to initialize the stack
struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
return stack;
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int data) {
if (isFull(stack)) {
printf("Stack overflow\n");
return;
}
stack->array[++stack->top] = data;
printf("%d pushed to stack\n", data);
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
return -1;
}
return stack->array[stack->top--];
}
// Main function to demonstrate stack operations
int main() {
struct Stack* stack = createStack();
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("%d popped from stack\n", pop(stack));
printf("%d popped from stack\n", pop(stack));
return 0;
}