C Exercises: Delete the nth element of a queue
11. Delete nth Element in Queue
Write a C program to delete the nth element of a queue.
Firstly, this code checks if the queue is empty or if the position to delete is invalid. If the position is valid, it deletes the first n-1 elements, then deletes the nth element by calling the dequeue() function.
Sample Solution:
C Code:
Output:
Insert some elements into the queue: Queue elements are: 1 2 3 4 5 Delete the 7th element of the said queue: Error: Invalid position Delete the 3rd element of the said queue: Queue elements are: 4 5
Flowchart
For more Practice: Solve these Related Problems:
- Write a C program to implement a queue and delete the nth element from the front without shifting all subsequent elements.
- Write a C program to implement a circular queue where deletion of the nth element adjusts the front pointer correctly.
- Write a C program to implement a queue that deletes an element based on a condition rather than its fixed position.
- Write a C program to delete every kth element from a queue repeatedly until the queue is empty (Josephus problem style).
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the minimum element in a queue.
Next: Sort a queue in ascending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.