Page 140 - DCAP407_DATA_STRUCTURE
P. 140
Unit 8: Queues
printf("\n");
}
} /*End of display() */
Output:
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 1
Enter an element to add in the queue: 25
Enter your choice: 1
Enter an element to add in the queue: 36
Enter your choice: 3
Elements in the queue: 25, 36
Enter your choice: 2
Element deleted from the queue is: 25
In this example:
1. The preprocessor directives #include are given. MAXSIZE is defined as 50
using the #define statement.
2. The queue is declared as an array using the declaration int queue_arr[MAX].
3. In the while loop, the different options are displayed on the screen and the
value entered in the variable choice is accepted.
4. The switch case compares the value entered and calls the method
corresponding to it. If the value entered is invalid, it displays the message
“Wrong choice”.
5. Insert method: The insert method inserts item in the queue. The if condition
checks whether the queue is full or not. If the queue is full, the “Queue
overflow” message is displayed. If the queue is not full, the item is inserted
in the queue and the rear is incremented by 1.
6. Delete method: The delete method deletes item from the queue. The if
condition checks whether the queue is empty or not. If the queue is empty,
the “Queue underflow” message is displayed. If the queue is not empty, the
item is deleted and front is incremented by 1.
7. Display method: The display method displays the contents of the queue. The
if condition checks whether the queue is empty or not. If the queue is not
empty, it displays all the items in the queue.
8.3 Representing Queue in Memory
The main attribute of a queue data structure is the fact that it permits accessibility only to the front and
back of the structure. Additionally, elements can be removed only from the front and added to the back.
In this way, the appropriate metaphor used to characterize queues is the idea of a checkout line.
LOVELY PROFESSIONAL UNIVERSITY 133