C++ program to implement queue operations using array

June 20, 2016

Aim:

To write a program to implement queue operations using array.

Algorithm:

Insert ( ):

QUEUE (FRONT, QUEUE, N, REAR, ITEM)

Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE. ITEM is the value to be inserted.

1.   [Check for overflow]

       If (REAR == N) Then

2.    Print: Overflow

3.    Else

4.    [Check if QUEUE is empty]

        If (FRONT and REAR == 0) Then

         (a) Set FRONT = 1

         (b) Set REAR = 1

5.    Else

6.    Set REAR = REAR + 1

         [End of Step 4 If]

7.    QUEUE[REAR] = ITEM

8.     Print: ITEM inserted

         [End of Step 1 If]

9.     Exit

Delete ( ):

QUEUE (FRONT, QUEUE, N, REAR, ITEM)

Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE.

  1.    [Check for underflow]

         If (FRONT == 0) Then

  1.     Print: Underflow

  2.     Else

  3.     ITEM = QUEUE[FRONT]

  4.     [Check if only one element is left]

         If (FRONT == REAR) Then

         (a) Set FRONT = 0

         (b) Set REAR = 0

  1.     Else

  2.     [Increment FRONT by 1]

         Set FRONT = FRONT + 1 

         [End of Step 5 If]

  1.     Print: ITEM deleted

         [End of Step 1 If]

  1.     Exit

 

Tagged in: ,
About Author
Adeeb C (219 Articles)

Adeeb C is the founder of this website. He is a blogger and web developer. And he writes about latest technology news, gadgets and windows since last four years.