C Queue using Arrays

beginner
5 min

C Queue using Arrays

Welcome to another exciting lesson on C Programming at CodeYourCraft! Today, we're going to dive into the world of data structures and learn how to create a Queue using Arrays. This concept is essential for understanding many real-world applications, such as operating system processes, print spoolers, and network traffic management.

Let's start by understanding what a Queue is: A Queue is a collection of entities (like numbers, characters, or objects) that follow a particular order, where elements are added at the end (rear) and removed from the beginning (front). This order follows the First-In-First-Out (FIFO) principle.

πŸ’‘ Pro Tip: Think of a line at a ticket counterβ€”people join the line at the end, and the first person in line is served first. That's exactly how a Queue works!

Creating a Simple Queue

Before we jump into the code, let's define the types we'll be using:

c
#include <stdio.h> #include <stdlib.h> typedef int QueueElementType;

In this example, we're using QueueElementType as an alias for int, but you can replace int with any data type based on your needs.

Now, let's write a simple C function that initializes a Queue:

c
#define MAX_QUEUE_SIZE 100 void initializeQueue(QueueElementType queue[], int *front, int *rear) { *front = *rear = -1; }

Here, we define a constant MAX_QUEUE_SIZE as 100. The initializeQueue function initializes both the front and rear pointers to -1, indicating that the Queue is empty.

πŸ“ Note: In our Queue implementation, the front and rear pointers point to the array index positions.

Enqueue (Adding an Element)

To add an element to our Queue, we'll create a function called enqueue:

c
void enqueue(QueueElementType queue[], int *front, int *rear, QueueElementType item) { if ((*rear + 1) % MAX_QUEUE_SIZE == *front) { printf("πŸ›‘ Queue is full. Cannot enqueue %d.\n", item); } else { (*rear) = (*rear + 1) % MAX_QUEUE_SIZE; queue[*rear] = item; if (*front == -1) *front = *rear; } }

In this function, we first check if the Queue is full (i.e., if adding an element would cause overflow). If the Queue is full, we print an error message and exit the function. Otherwise, we increment the rear pointer, store the new element at the indicated array position, and update the front pointer if necessary (since this is the first element being added).

Dequeue (Removing an Element)

To remove an element from our Queue, we'll create a function called dequeue:

c
QueueElementType dequeue(QueueElementType queue[], int *front, int *rear) { if (*front == -1) { printf("πŸ›‘ Queue is empty. Cannot dequeue.\n"); return -1; } else { QueueElementType item = queue[*front]; (*front) = (*front + 1) % MAX_QUEUE_SIZE; if (*front == *rear) *rear = -1; return item; } }

In this function, we first check if the Queue is empty. If the Queue is empty, we print an error message and return -1. Otherwise, we store the front element in a variable, update the front pointer, and update the rear pointer if necessary (since the Queue is now empty).

πŸ’‘ Pro Tip: To test our Queue implementation, you can enqueue and dequeue elements using the following code snippets:

c
initializeQueue(queue, &front, &rear); enqueue(queue, &front, &rear, 10); enqueue(queue, &front, &rear, 20); enqueue(queue, &front, &rear, 30); printf("πŸ“ Dequeued: %d\n", dequeue(queue, &front, &rear)); printf("πŸ“ Dequeued: %d\n", dequeue(queue, &front, &rear)); printf("πŸ“ Dequeued: %d\n", dequeue(queue, &front, &rear)); printf("πŸ“ Dequeued: %d\n", dequeue(queue, &front, &rear));

This code initializes our Queue, enqueues three elements, and dequeues them one by one.

Quick Quiz
Question 1 of 1

Which function is responsible for removing an element from the Queue?

Wrapping Up

In this lesson, we've learned how to create a simple Queue using arrays in C programming. We've discussed the First-In-First-Out (FIFO) principle and created functions to initialize, enqueue, and dequeue elements in our Queue.

🎯 Practice: Try extending this Queue implementation by adding functions to check if the Queue is empty, full, or to peek at the front element without removing it.

Stay tuned for more in-depth C programming lessons, and remember to practice, practice, practice! Happy coding! πŸš€πŸ’»πŸ“š