#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct {
int arr[MAX];
int top;
} Stack;
typedef struct {
int arr[MAX];
int front;
int rear;
} Queue;
void stack_init(Stack* s) {
s->top = -1;
}
int stack_is_empty(Stack* s) {
return s->top == -1;
}
int stack_is_full(Stack* s) {
return s->top == MAX - 1;
}
void stack_push(Stack* s, int val) {
if (stack_is_full(s)) {
printf("Stack overflow\n");
return;
}
s->arr[++s->top] = val; // push val on top
}
int stack_pop(Stack* s) {
if (stack_is_empty(s)) {
printf("Stack underflow\n");
return -1;
}
return s->arr[s->top--]; // pop top val
}
void queue_init(Queue* q) {
q->front = 0;
q->rear = -1;
}
int queue_is_empty(Queue* q) {
return q->rear < q->front;
}
int queue_is_full(Queue* q) {
return q->rear == MAX - 1;
}
void queue_enqueue(Queue* q, int val) {
if (queue_is_full(q)) {
printf("Queue overflow\n");
return;
}
q->arr[++q->rear] = val; // add val at rear
}
int queue_dequeue(Queue* q) {
if (queue_is_empty(q)) {
printf("Queue underflow\n");
return -1;
}
return q->arr[q->front++]; // remove val from front
}
void print_stack(Stack* s) {
printf("Stack (bottom to top): ");
for (int i = 0; i <= s->top; i++) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
void print_queue(Queue* q) {
printf("Queue (front to rear): ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->arr[i]);
}
printf("\n");
}
int main() {
Stack s;
Queue q;
stack_init(&s);
queue_init(&q);
// Stack operations
stack_push(&s, 1);
stack_push(&s, 2);
int popped = stack_pop(&s);
// Queue operations
queue_enqueue(&q, 1);
queue_enqueue(&q, 2);
int dequeued = queue_dequeue(&q);
print_stack(&s);
print_queue(&q);
return 0;
}
s->arr[++s->top] = val; // push val on top
add element on top of stack
return s->arr[s->top--]; // pop top val
remove element from top of stack
q->arr[++q->rear] = val; // add val at rear
add element at rear of queue
return q->arr[q->front++]; // remove val from front
remove element from front of queue
Stack (bottom to top): 1
Queue (front to rear): 2