Queues Programming Practice

"You are best served when you are in Queue. Patience is your friend there. "

— Await your turn!

Fundamentals

  1. Consider an initially empty queue of size 6. Trace the state of the queue (showing the elements, front pointer, and rear pointer) after the following sequence of operations:
  2. enqueue(10)
    enqueue(20)
    enqueue(30)
    dequeue()
    enqueue(40)
    enqueue(50)
    dequeue()
    enqueue(60)
        

Simple Queue (Linear Queue) using Arrays

  1. Write a complete C program to implement a simple queue using an array. Your program should include functions for insert(), delete(), and display().
  2. Write the algorithm for the enqueue() and dequeue() operations for a simple queue.

Circular Queues

  1. Consider an empty circular queue of size 5. Trace the state of the queue (front, rear, count, and the array contents) after the following sequence of operations:
  2. enqueue(10)
    enqueue(20)
    enqueue(30)
    dequeue()
    enqueue(40)
    enqueue(50)
    dequeue()
    enqueue(60)  
    enqueue(70)
        
  3. Write the C code for the add (enqueue) and delete (dequeue) operations for a circular queue that uses a count variable.

Priority Queues

  1. Given an ascending priority queue with the elements
    {10, 25, 35, 50}
    show the step-by-step process of inserting the element 30 into the queue.
  2. Write a C function insert_by_priority() that inserts an element into an ascending priority queue implemented with an array.

Deque (Double-Ended Queue)

  1. Write C functions to implement the four core operations of a Deque using an array: insert_front(), insert_rear(), delete_front(), and delete_rear().
  2. Explain the logic for handling "wrap-around" when inserting at the front if front is at index 0.

Advanced Implementations & Concepts

  1. Multiple Stacks/Queues in an Array:
    Explain the strategy for implementing two stacks in a single array. What is the overflow condition (topA == topB - 1)?
  2. For implementing 'n' stacks in an array of size SIZE, what is the formula to initialize the top and boundary for each stack? If SIZE is 20 and you need to create 4 stacks, what would be the initial top and boundary values for each stack?
  3. Write the general push() and pop() logic for a specific stack i when 'n' stacks are implemented in one array.
  4. Stack and Queue Implementations:
    Describe the algorithm to implement a queue using two stacks. Explain the steps for the enqueue and dequeue operations.
  5. Explain the two methods for implementing a stack using two queues:
    • Making the push operation costly.
    • Making the pop operation costly.
  6. Trace the state of the two queues when implementing a stack (with a costly push operation) for the sequence: push(5), push(10), pop(), push(20).

Find it difficult?

Don’t lose heart, don’t be under confident, just be consistent in your preparation and be sure of everything you’ve studied. You can request a class so that we can help you understand this topic.

Feel Confident?

Your first step in learning any new topic is to be aware of your strengths and weaknesses. Once you know this, your self-preparation can be meaningful and result-oriented. Attempt a quiz to get tested.