0
17kviews
Write an Algorithm for implementing queue using array.
2 Answers
2
746views


Queue using Arrays : -


Algorithm : -

Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]

Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]

Step 3: Set QUEUE[REAR] = NUM

Step 4: EXIT



Program Implementation : -

// Queue using Arrays 
#include<iostream>
using namespace std;
#define n 20 
class queue{
    int* arr;
    int front;
    int back;
    public:
    queue(){
        arr = new int[n];
        front = -1;
        back  = -1;
    }
    void push(int x){            // push = Enqueue
        if (back==n-1){
            cout<<"Queue Overflow"<<endl;
            return;
        }
        back++;
        arr[back] = x;
        if (front==-1){
            front++;
        }
    }
    void pop(){                // pop = Dequeue
        if (front==-1 || front>back){
            cout<<"No element in queue"<<endl;
            return;
        }
        front++;
    }
    int peek(){
        if (front==-1 || front>back){
            cout<<"No elements in queue"<<endl;
            return -1;
        }
        return arr[front];
    }
    bool empty(){
        if (front==-1 || front>back){
            return true;
        }
        return false;
    }
};
int main(){
    queue q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    cout<<q.peek()<<endl;
    q.pop();
    cout<<q.peek()<<endl;
    q.pop();
    cout<<q.peek()<<endl;
    q.pop();
    cout<<q.peek()<<endl;
    q.pop();
    cout<<q.empty()<<endl;
    return 0;
}
//Enjoy Coding...
1
778views

Queue using Array

  • A queue is a data structure that can be implemented using linear arrays or a one-dimensional array.
  • The implementation of queue data structure using a one-dimensional array is very simple and easy.
  • This requires a one-dimensional array of well-defined size.
  • Then insert or delete operation can be performed on that array by using the First In First Out (FIFO) property.
  • To do this two variables Front and Rear are used.
  • Front and Rear variables show the exact position from where insertion and deletion operations were performed on a one-dimensional array representation of the queue.
  • Initially, both the values of variable Front and Rear are '-1' when the queue is empty.

Queue




Algorithm for Insert Operation

When a new element value is need to be inserted into the queue, then the Rear value is incremented by one, and then after the new element value is inserted at that position.

  • The enQueue() function is used to insert a new element into the queue data structure.
  • The new element is always inserted in the Rear position.
  • The enQueue() function takes one integer value as a parameter and inserts that value into the queue.

Let's look at the Algorithm for insert or enQueue.


Step 1 -

IF (REAR == Size - 1) // Condition for Overflow

Write Queue is in Overflow condition therefore, insertion is not possible

END OF IF

Step 2 -

IF ((FRONT == -1) and (REAR = -1)) // Inserting an element in an empty queue

FRONT = REAR = 0

ELSE

SET REAR = REAR + 1 // Increment Rear

END OF IF

Step 3 -

SET QUEUE [REAR] = element // Assign the inserted element to the queue

Step 4 -

END Enqueue


  • The above algorithm first checks if the queue is full or not.
  • If the queue is full, then it will print "Queue Overflow" and exit.
  • Otherwise, increment the variable REAR by 1.
  • Finally, assign the new value to be inserted into the array of queue.



Algorithm for Delete Operation

When a new value is need to be deleted from the queue, then delete the value which is at the Front position and then increments the Front value by one.

  • The deQueue() function is used to delete an element from the queue.
  • In a queue, the element is always deleted from the Front position.
  • The deQueue() function does not take any value as a parameter.

Let's look at the Algorithm for delete or deQueue.


Step 1 -

IF ((FRONT == -1) or (FRONT > REAR)) // Condition for Underflow

Write Queue is in Underflow condition therefore, deletion is not possible

ELSE

SET Element = QUEUE [FRONT]

SET FRONT = FRONT + 1 // Increment Front

END OF IF

Step 2 -

End Dequeue


  • The above algorithm first checks whether the value of Front is -1 or the value of Front is greater than the rear.
  • If the condition is true then it will print "Queue Underflow" and exit.
  • Otherwise, keep increasing the value of the variable Front and return the value stored at the Front end of the queue.
Please log in to add an answer.