Stacks and Queues in C


Introduction

Stacks and queues are linear data structures used in computer science and C programming for various applications. Stacks follow the Last In, First Out (LIFO) principle, while queues adhere to the First In, First Out (FIFO) principle. In this guide, we'll introduce you to stacks and queues in C and provide sample code to illustrate their usage.


Stacks

Stacks are used to store elements in a linear order, and the last element inserted is the first one to be removed. Key operations include push (to add an element) and pop (to remove the top element).


Sample Code for Stacks

#include <stdio.h>
#define MAX_SIZE 10
// Define a stack structure
struct Stack {
int items[MAX_SIZE];
int top;
};
void initialize(struct Stack* stack) {
stack->top = -1;
}
int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int item) {
if (!isFull(stack)) {
stack->items[++stack->top] = item;
} else {
printf("Stack is full. Cannot push %d.\\n", item);
}
}
int pop(struct Stack* stack) {
if (!isEmpty(stack)) {
return stack->items[stack->top--];
} else {
printf("Stack is empty.\\n");
return -1;
}
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
printf("Popped: %d\\n", pop(&stack));
return 0;
}

Queues

Queues are used to store elements in a linear order, and the first element inserted is the first one to be removed. Key operations include enqueue (to add an element) and dequeue (to remove the front element).


Sample Code for Queues

#include <stdio.h>
#define MAX_SIZE 10
// Define a queue structure
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
void initialize(struct Queue* queue) {
queue->front = -1;
queue->rear = -1;
}
int isFull(struct Queue* queue) {
return (queue->rear == MAX_SIZE - 1 && queue->front == 0) || (queue->rear == (queue->front - 1) % (MAX_SIZE - 1));
}
int isEmpty(struct Queue* queue) {
return queue->front == -1;
}
void enqueue(struct Queue* queue, int item) {
if (!isFull(queue)) {
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->items[queue->rear] = item;
} else {
printf("Queue is full. Cannot enqueue %d.\\n", item);
}
}
int dequeue(struct Queue* queue) {
if (!isEmpty(queue)) {
int item = queue->items[queue->front];
if (queue->front == queue->rear) {
queue->front = queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
return item;
} else {
printf("Queue is empty.\\n");
return -1;
}
}
int main() {
struct Queue queue;
initialize(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
printf("Dequeued: %d\\n", dequeue(&queue));
return 0;
}

Conclusion

Stacks and queues are essential data structures in C programming, used for a variety of applications, including algorithmic solutions and system-level tasks. This guide introduced you to the concepts of stacks and queues and provided sample code for each data structure. As you continue your C programming journey, you'll find stacks and queues to be valuable tools for solving a wide range of problems.