Page 153 - DCAP407_DATA_STRUCTURE
P. 153
Data Structure
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice:
2
Deleted number is 10
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice:
2
Deleted number is 20
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice:
2
QUEUE UNDERFLOW
In this example:
1. The header files namely, stdio and malloc are included.
2. A structure named queue is created which consists of three variables namely,
value, PRI, and next. The variable value holds the value of the element. The
PRI holds the element priority value and next is a pointer variable that points
to the next element in the queue. Four objects are declared namely, F, q, tmp,
and new to access the structure elements.
3. Three functions are created namely, ins(), del(), and disp(). The user has to
select an appropriate function to perform.
4. The switch statement is used to call the ins(), del(), and disp() functions.
5. When the user enters 1, the ins() function is called. This function allocates
memory of capacity 10 for the queue using malloc function. Then, the user
enters the element to be inserted into the queue, and its priority. The value of
the element entered is stored in value and its priority is stored in PRI. The if
loop checks if the F value is equal to NULL or the priority of the entered
element is less than the priority of the first element. If either of the condition
is true, then the element entered is stored in the second position of the queue.
Else, the value of F is assigned to q. The if loop checks if the third position of
the queue is not equal to NULL and the priority of the second element is less
than the third. If the condition is true, then the second element is stored in the
third position and the third element is stored in the second position. The loop
continues to check for the priority of all the elements in the queue and stores
them accordingly.
146 LOVELY PROFESSIONAL UNIVERSITY