#17.Journey to DSA in C++

#17.Journey to DSA in C++

Β·

6 min read

πŸš€ Journey into Data Structures and Algorithms (DSA) with C++: Unveiling the Magic of Queues

Hello there, aspiring coders! πŸ‘‹ Welcome to an exciting journey into the captivating world of Data Structures and Algorithms (DSA) using the power of C++. Today, we're about to embark on a quest to explore one of the most intriguing concepts in the realm of DSAβ€”Queues.

Introducing the Enigmatic Queue πŸšΆβ€β™‚οΈπŸšΆβ€β™€οΈ

Imagine a line of people waiting to board a bus. The person who arrives first is the first to step onto the bus, and the person who arrives last is the last in line. This is precisely how a queue works in the world of programming. A queue is an ordered collection of elements, where the element that's been in the queue the longest is the first to be processed (FIFO - First-In-First-Out).

Queues are everywhere in computer science, from managing tasks in an operating system to handling requests on a web server. Understanding them is like holding the key to efficient, orderly data management.

Types of Queues 🧐

1. Simple Queue:

This is the fundamental type of queue, as we just discussed. Elements are inserted at the rear, and they are removed from the front.

2. Priority Queue:

In a priority queue, elements have associated priorities. The element with the highest priority is the first to be removed. It's like granting VIP access to certain elements.

3. Circular Queue:

A circular queue is a variation of the simple queue with a twist. When the rear reaches the end, it wraps around to the front. It's like a circular track, ensuring no space goes to waste.

4. Double-Ended Queue (Deque):

A deque allows insertion and removal of elements from both ends, making it highly versatile. It's like having doors at the front and back of a bus.

Concepts and Techniques 🀯

Now, let's dive into some of the essential concepts and techniques related to queues:

1. Enqueue and Dequeue:

  • Enqueue: This is the process of adding an element to the rear of the queue. It's like a person joining the line to board the bus.

  • Dequeue: Dequeue is the operation that removes the element from the front of the queue. It's akin to the first person stepping onto the bus.

2. Front and Rear:

  • Front: The front of the queue is where elements are removed or dequeued.

  • Rear: The rear of the queue is where new elements are added or enqueued.

3. Empty and Full Queue:

  • An empty queue has no elements.

  • A full queue is one where the rear has reached a position immediately before the front. In the case of a circular queue, this means it's wrapped around.

4. Breadth-First Search (BFS):

BFS is an algorithm that uses a queue to explore nodes level by level in a graph or tree. It's like searching for treasures on a map systematically.

Code in C/C++

#include<stdio.h>
#define SIZE 5

//Basic value initialization
int queue[SIZE], front = -1, rear = -1;

void Enqueue(int val){
    if(rear == SIZE-1)
        printf("Encountered Overflow Condition\n");
    else{
        //When adding initial element
        if(front == -1)
            front = 0;

        rear++;
        queue[rear] = val;
        printf("%d was enqueued\n",val);
   }
}
void Dequeue(){
    if(front == -1)
        printf("Underflow condition\n");
    else{
        printf("Dequeued : %d\n", queue[front]);
        front++;

        //resetting the queue when last item is Dequeued 
        if(front > rear)
            front = rear = -1;
   }
}
void display(){
    if(rear == -1)
        printf("\nQueue is empty");
   else{
        int i;
        printf("\nNow, we are printing the Queue :");

        for(i=front; i<=rear; i++)
            printf("%d ",queue[i]);
   }
}

int main()
{
   Enqueue(10);
   Enqueue(20);
   Enqueue(30);
   Enqueue(40);

   Dequeue();
   Dequeue();

   display();
   return 0;
}

My C program appears to be an implementation of a basic queue data structure with enqueue (adding elements), dequeue (removing elements), and display functionalities. Let me provide an overview of the code's functionality:

  1. I've defined a constant SIZE for the maximum size of the queue. In this case, it's set to 5.

  2. I've declared an integer array queue of size SIZE to represent the queue and two integer variables front and rear to keep track of the front and rear positions of the queue.

  3. The Enqueue function is used to add elements to the queue. It checks for overflow conditions and ensures that front is properly initialized. If the queue is not full, it increments rear and adds the value to the queue array.

  4. The Dequeue function is used to remove elements from the queue. It checks for underflow conditions and prints the element removed. It also resets the front and rear when the last item is dequeued, which signifies an empty queue.

  5. The display function prints the current elements in the queue. It checks if the queue is empty and, if not, iterates through the elements from front to rear and prints them.

  6. In the main function, I perform some basic enqueue and dequeue operations to test my queue implementation.

Here's a summary of the operations in my code:

  • I enqueue values 10, 20, 30, and 40 into the queue.

  • I dequeue two values (10 and 20).

  • Finally, I display the elements remaining in the queue, which would be 30 and 40.

My code provides a simple and functional implementation of a queue in C. Queues are essential in various computer science and programming scenarios, such as task scheduling and managing data with a FIFO (First-In-First-Out) order.

Why Queue Mastery Matters 🌟

You might be wondering, "Why should I invest my time in mastering queues in DSA?" Well, here's the scoop:

  1. Efficient Data Handling: Queues are incredibly efficient in managing data where order matters, such as task scheduling or managing requests in a web server.

  2. Algorithms and Data Structures: Understanding queues is not just about queues themselves. It's about mastering the concepts that apply to a wide range of algorithms and data structures.

  3. Problem-Solving Skills: Learning how to use queues effectively enhances your problem-solving skills and equips you to tackle diverse challenges in the world of programming.

  4. Real-World Applications: Queues are everywhere, from computer systems to everyday life. Mastering queues means you can model real-world scenarios in your code.

Stay Curious and Experiment! πŸ€“πŸ”¬

As we wrap up our journey into the captivating world of queues, I encourage you to stay curious and experiment. Create programs that implement queues, explore different types of queues, and test their performance in various scenarios.

In our next session, we'll dive into more thrilling aspects of DSA, exploring new data structures and algorithms that will expand your coding horizons. Get ready for more magic in the world of C++ and DSA!

Until then, keep coding, stay curious, and remember that every line of code you write is a step closer to becoming a coding wizard! πŸ’»πŸŒŸβœ¨

Β