Overview of Other Data Structures (Stacks, Queues)
Data structures are essential components in computer science, as they allow us to store, organize, and manipulate data efficiently. In this article, we will discuss two fundamental linear data structures in C: Stacks and Queues. These data structures are widely used in various algorithms and real-world applications.
1. Stacks
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This means that the last element added to the stack will be the first one to be removed. It is similar to a stack of plates, where the last plate placed on top is the first one to be taken off.
Operations on Stack
A stack supports two main operations:
- push: Adds an element to the top of the stack.
- pop: Removes the element from the top of the stack.
Example of a Stack in C
#include <stdio.h> #include <stdlib.h> #define MAX 5 struct Stack { int arr[MAX]; int top; }; // Initialize the stack void initStack(struct Stack* stack) { stack->top = -1; } // Push an element onto the stack void push(struct Stack* stack, int value) { if (stack->top == MAX - 1) { printf("Stack Overflow\n"); } else { stack->arr[++(stack->top)] = value; printf("%d pushed onto stack\n", value); } } // Pop an element from the stack int pop(struct Stack* stack) { if (stack->top == -1) { printf("Stack Underflow\n"); return -1; } else { return stack->arr[(stack->top)--]; } } int main() { struct Stack stack; initStack(&stack); 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; }
In this example, we define a stack using an array and implement push
and pop
operations. The stack follows the LIFO principle, meaning the last element pushed onto the stack will be the first one popped off.
2. Queues
A queue is another linear data structure that follows the FIFO (First In, First Out) principle. In a queue, the first element added is the first one to be removed. It is similar to a queue at a ticket counter, where the first person to join the line is the first one to be served.
Operations on Queue
A queue supports the following operations:
- enqueue: Adds an element to the end of the queue.
- dequeue: Removes the element from the front of the queue.
Example of a Queue in C
#include <stdio.h> #include <stdlib.h> #define MAX 5 struct Queue { int arr[MAX]; int front; int rear; }; // Initialize the queue void initQueue(struct Queue* queue) { queue->front = -1; queue->rear = -1; } // Enqueue an element into the queue void enqueue(struct Queue* queue, int value) { if (queue->rear == MAX - 1) { printf("Queue Overflow\n"); } else { if (queue->front == -1) { queue->front = 0; } queue->arr[++(queue->rear)] = value; printf("%d enqueued to queue\n", value); } } // Dequeue an element from the queue int dequeue(struct Queue* queue) { if (queue->front == -1) { printf("Queue Underflow\n"); return -1; } else { int value = queue->arr[queue->front]; if (queue->front == queue->rear) { queue->front = queue->rear = -1; } else { queue->front++; } return value; } } int main() { struct Queue queue; initQueue(&queue); enqueue(&queue, 10); enqueue(&queue, 20); enqueue(&queue, 30); printf("%d dequeued from queue\n", dequeue(&queue)); printf("%d dequeued from queue\n", dequeue(&queue)); return 0; }
In this example, we define a queue using an array and implement enqueue
and dequeue
operations. The queue follows the FIFO principle, meaning the first element enqueued will be the first one dequeued.
Conclusion
Stacks and queues are fundamental data structures that are used in a wide variety of applications. The stack follows the LIFO principle, making it suitable for tasks like function calls and expression evaluation, while the queue follows the FIFO principle, which is useful in scheduling and managing resources. Understanding how to implement and use these data structures is essential for solving problems efficiently in C programming.