C++ Queue Data Structure Implementation

Classified in Computers

Written on in English with a size of 6.1 KB

This document presents a C++ implementation of a Queue data structure, demonstrating its core functionalities and operations. The Queue is implemented using a linked list, adhering to the First-In, First-Out (FIFO) principle.

Queue Class Definition

The Queue class encapsulates the data members and member functions necessary to manage a queue. Note that the Node struct/class is assumed to be defined elsewhere, typically as struct Node { double info; Node* link; };.

class Queue
{
private:
    Node* front;
    Node* rear;
    int count;
    double info; // This member 'info' within the Queue class itself seems unused in the provided code.
public:
    // Constructor: Should be 'Queue();' without 'void'. Implementation is missing.
    Queue();

    // Member Functions
    bool is_empty();
    void enqueue(double); // Corrected type to 'double' to match implementation
    void dequeue(); // Corrected type to 'void' to match implementation's primary effect
    double front_data();
    double rear_data();
    void display();
    int size(){return count;}
};

Main Function: Interactive Queue Operations

The main function provides an interactive console interface to demonstrate the Queue class functionalities, allowing users to perform various operations.

int main()
{
    int flag, item; // 'item' variable is declared but unused.
    double entry[5];
    Queue ql; // Object of Queue class

    while (1)
    {
        cout << "Select Your Operation:\n";
        cout << "1. Create an Empty Queue" << endl;
        cout << "2. Add 5 Doubles to the Queue" << endl;
        cout << "3. Print Front Element of the Queue" << endl;
        cout << "4. Print Rear Element of the Queue" << endl;
        cout << "5. Print All Elements and Remove from the Queue" << endl;
        cin >> flag;

        switch(flag)
        {
        case 1:
            // Manual initialization, ideally handled by a constructor.
            ql.front = NULL;
            ql.rear = NULL;
            ql.count = 0;
            cout << "Empty Queue created\n";
            break;
        case 2:
            cout << "Enter values to be inserted into the queue (5 doubles): ";
            for(int i=0; i<5; i++)
            {
                cin >> entry[i];
                ql.enqueue(entry[i]);
            }
            ql.display();
            break;
        case 3:
            cout << "Front element: " << ql.front_data() << "\n";
            break;
        case 4:
            cout << "Rear element: " << ql.rear_data() << "\n";
            break;
        case 5:
            // The original code had 'cout << ql.dequeue() << "\n";'.
            // Since 'dequeue' now returns 'void' (as its primary action is printing the element),
            // directly printing its return value is not possible. The call is adjusted.
            ql.dequeue();
            break;
        default:
            cout << "Wrong Choice" << endl;
        }
    }
    // Missing 'return 0;' for a proper main function exit.
}

Queue Member Function Implementations

void Queue::enqueue(double value)

Adds an element to the rear of the queue.

void Queue::enqueue(double value) // Corrected parameter type to 'double'
{
    Node *tmp;
    tmp = new Node; // More idiomatic C++ than 'new (struct Node)'
    tmp->info = value;
    tmp->link = NULL;
    if (front == NULL)
        front = tmp;
    else
        rear->link = tmp;
    rear = tmp;
    // Missing 'count' increment here.
}

void Queue::display()

Prints all elements currently in the queue from front to rear.

void Queue::display()
{
    Node *ptr; // Corrected type capitalization from 'node' to 'Node'
    ptr = front;
    if (front == NULL)
        cout << "Queue is empty" << endl;
    else
    {
        cout << "Queue elements: " << endl;
        while (ptr != NULL)
        {
            cout << ptr->info << " ";
            ptr = ptr->link;
        }
        cout << endl;
    }
}

double Queue::front_data()

Retrieves the element at the front of the queue without removing it.

double Queue::front_data()
{
    Node *ptr; // Corrected type capitalization from 'node' to 'Node'
    ptr = front; // This 'ptr' is declared but not used in this function's logic.
    if (front == NULL)
    {
        cout << "Queue is empty" << endl;
        return 0.0; // Returning 0.0 for an empty queue might be misleading for double values.
    }
    else
    {
        return front->info;
    }
    // The 'return 0;' at the end of the original function is unreachable.
}

double Queue::rear_data()

Retrieves the element at the rear of the queue without removing it.

double Queue::rear_data()
{
    Node *ptr; // Corrected type capitalization from 'node' to 'Node'
    ptr = front; // This 'ptr' is declared but not used in this function's logic.
    if (front == NULL)
    {
        cout << "Queue is empty" << endl;
        return 0.0; // Returning 0.0 for an empty queue might be misleading for double values.
    }
    else
    {
        return rear->info;
    }
    // The 'return 0;' at the end of the original function is unreachable.
}

void Queue::dequeue()

Removes the element from the front of the queue.

void Queue::dequeue()
{
    Node *tmp; // Corrected type capitalization from 'node' to 'Node'
    if (front == NULL)
        cout << "Queue Underflow" << endl;
    else
    {
        tmp = front;
        cout << "Element Deleted: " << tmp->info << endl;
        front = front->link;
        delete tmp; // Corrected from 'free(tmp)' to 'delete tmp' for C++ memory management
        // Missing 'count' decrement here.
    }
}

Related entries: