Practice Top 30 C++ Programming Questions on Queue
Q1. Write a program to implement a queue using the std::queue library. Perform the following operations:
1. Insert the integers 10, 20, and 30 into the queue.
2. Remove the front element.
3. Display the front element and the size of the queue.
Expected Output:
Front element: 20
Queue size: 2
Q2. Write a program to create an empty queue and check whether it is empty. Then, add one element to the queue and check again.
Expected Output:
Queue is empty: Yes
Queue is empty after adding an element: No
Q3. Write a program to reverse the elements of a queue. Use the std::queue library and std::stack to reverse the order of elements.
Input:
Elements in queue: 1, 2, 3, 4, 5
Expected Output:
Queue after reversing: 5 4 3 2 1
Q4. Write a program to manage a queue of strings. Add three names (Alice, Bob, and Charlie) to the queue, remove one, and display the remaining elements.
Input:
No input is required.
Expected Output:
Remaining names in queue: Bob Charlie
Q5. Write a program to calculate the sum of all integers in a queue.
Input:
Elements in queue: 4, 7, 1, 8
Expected Output:
Sum of elements: 20
Q6. Write a program to insert elements 5, 10, 15 into a queue. Remove two elements and display the size of the queue.
Input:
No input is required.
Expected Output:
Queue size: 1
Q7. Write a program to peek at the front and back elements of a queue. Insert 10, 20, 30 into the queue.
Input:
No input is required.
Expected Output:
Front element: 10
Back element: 30
Q8. Write a program to merge two queues into one.
Queue 1: 1, 2, 3
Queue 2: 4, 5, 6
Expected Output:
Merged queue: 1 2 3 4 5 6
Q9. Write a program to count the number of even numbers in a queue.
Queue: 3, 6, 8, 5, 10
Expected Output:
Number of even elements: 3
Q10. Write a program to check if a specific element exists in a queue.
Queue: 12, 24, 36, 48
Check for 24.
Expected Output:
Element 24 is in the queue.
Q11. Write a program to continuously add numbers to a queue until the user inputs 0. Display all elements.
Input:
4 7 9 0
Expected Output:
Queue: 4 7 9
Q12. Write a program to find the maximum element in a queue.
Queue: 3, 15, 7, 10, 25
Expected Output:
Maximum element: 25
Q13. Write a program to find the minimum element in a queue.
Queue: 8, 4, 11, 2, 9
Expected Output:
Minimum element: 2
Q14. Write a program to print the queue in reverse order without modifying it.
Queue: 3, 6, 9, 12
Expected Output:
Reverse order: 12 9 6 3
Q15. Write a program to display alternate elements of a queue.
Queue: 5, 10, 15, 20, 25
Expected Output:
Alternate elements: 5 15 25
Q16. Write a program to check if a queue forms a palindrome sequence.
Queue: 1, 2, 3, 2, 1
Expected Output:
The queue forms a palindrome.
Q17. Simulate a circular queue using the std::deque library. Insert 10, 20, 30, remove one element, and add 40.
Expected Output:
Queue: 20 30 40
Q18. Interleave the elements of two queues.
Queue 1: 1, 3, 5
Queue 2: 2, 4, 6
Expected Output:
Interleaved queue: 1 2 3 4 5 6
Q19. Write a program to reverse the first n elements of a queue.
Queue: 10, 20, 30, 40, 50
n = 3
Expected Output:
Queue after reversing first 3 elements: 30 20 10 40 50
Q20. Write a program to manage a queue of characters. Insert A, B, C, D, remove two, and display the remaining elements.
Expected Output:
Queue: C D
Q21. Sort the elements of a queue in ascending order.
Queue: 5, 2, 8, 1, 3
Expected Output:
Sorted queue: 1 2 3 5 8
Q22. Implement a priority queue using std::priority_queue. Insert 5, 3, 8, 1 and display the elements.
Expected Output:
Priority queue: 8 5 3 1
Q23. Write a program to double all the elements in a queue.
Queue: 2, 4, 6, 8
Expected Output:
Updated queue: 4 8 12 16
Q24. Find the second largest element in a queue.
Queue: 12, 45, 7, 10, 30
Expected Output:
Second largest element: 30
Q25. Calculate the sum of all odd numbers in a queue.
Queue: 1, 4, 5, 6, 9
Expected Output:
Sum of odd elements: 15
Q26. Write a recursive function to display all elements of a queue.
Queue: 7, 14, 21
Expected Output:
Queue: 7 14 21
Q27. Write a program to copy the contents of one queue into another.
Queue: 10, 20, 30
Expected Output:
Copied queue: 10 20 30
Q28. Simulate a queue with a fixed size limit. Insert elements until the limit is reached.
Limit: 3
Elements: 10, 20, 30, 40
Expected Output:
Queue is full. Cannot insert 40.
Q29. Write a program to remove duplicate elements from a queue.
Queue: 10, 20, 10, 30, 20
Expected Output:
Queue after removing duplicates: 10 20 30
Q30. Use a queue to generate the first n Fibonacci numbers.
Input n = 5
Expected Output:
Fibonacci sequence: 0 1 1 2 3
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!