C++ program to implement queue operations using array

20-06-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: ,