Join Regular Classroom : Visit ClassroomTech

Queue implementation using Array – Codewindow

Hot Topics

Data Structure

Queue using array

Algorithm

  1. Create an array of size n.

  2. Initialize two variable qfront and rear as 0 which tells us that the queue is empty.

  3. qfront gives us the index of the first element and rear gives the index till which the element is stored

  4. To push an element in queue we must check that rear shouldn’t reach to the last position of an array if it does then the queue is full.If the rear didn’t reach the last position of array then we can insert element at arr[rear] and increase rear by 1.

  5. To remove element from queue we must check whether the queue is empty which can be got when both front and rear variables are at same index.If the front and rear variables are not at same index then we can remove element from queue.

  6. Get the element at the front of queue to see which element is present at first in queue.

  7. Print all the elements until both front and rear variables point to same location to see the elements present in queue.

 

Code:

class Queue
{
    /*defining the the data members of queue with the help of which we
will create queue*/
    int *arr;
    int qfront;
    int rear;
    int size;

public:
    // constructor function where the queue is created
    Queue(int s)
    {
        size = s;
        arr = new int[size];
        qfront = rear = 0;
    }

    // function to enter elements into queue
    void push(int data)
    {
        // condition checking for no empty space in queue
        if (rear == size)
            std::cout << std::endl<< "Queue is full and no element can be inserted" << std::endl;
        // when empty space is there we are inserting element at //the index of array where rear is pointing
        else
        {
            arr[rear] = data;
            // after inserting we are increasing index as that
            // index in array is filled up
            rear++;
        }
    }

    // function to remove element from queue
    void pop()
    {
        // checking if the queue is empty then we can’t remove
        // element
        if (qfront == rear)
            return;
        // when there is atleast one or more than one element is
        // present in queue.
        else
        {
            // getting the element at the front of queue and
            // storing it in a variable 
            int ans = arr[qfront];
            // reassigning that position to -1
            arr[qfront] = -1;
            // reassigning index after removing the element form
            // last index
            qfront++;
            // checking again if the front and rear variable
            // pointing to same index
            if (qfront == rear)
                qfront = rear = 0;
            std::cout << std::endl<< "Popped element is : "<<ans<<std::endl;
        }
    }

    // function to get the element at the front of queue
    int front()
    {
        if (qfront == rear)
            return -1;
        else
            return arr[qfront];
    }

    void display()
    {
        if (qfront == rear)
        {
            std::cout << "Queue is empty" << std::endl;
            return;
        }
        else
        {
            for (int i = qfront; i < rear; i++)
                std::cout << arr[i] <<” “;
            return;
        }
    }
};

int main(void)
{
    // Create a queue of capacity 4
    Queue q(4);

    // print Queue elements
    q.display();

    // inserting elements in the queue
    q.push(20);
    q.push(30);
    q.push(40);
    q.push(50);

    // print Queue elements
    q.display();

    // insert element in the queue
    q.push(60);

    // print Queue elements
    q.display();

    q.pop();
    q.pop();

    std::cout << "After deleting two elements" << std::endl;

    // print Queue elements
    q.display();

    // print front of the queue
    q.front();

    return 0;
}
Output
Queue is empty
20 30 40 50 
Queue is full and no element can be inserted
20 30 40 50 
Popped element is:20

Popped element is:30
After deleting two elements
40 50 

Nagarro Solved

Automata Fixing

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories