You are on page 1of 1

3. This question deals with the implementation of a queue using stacks.

Assume you have a stack with operations: push(), pop(), isEmpty(). How would you
use these stack operations to simulate a queue, in particular, the operations enqueue()
and dequeue()?
Hints: Use two stacks one of which is the main stack and one is a temporary one.
For the enqueue operation you can use the temporary stack to show your final answer.
For the dequeue operation you can show your final answer in the main stack after
using the temporary stack.
NB: The rear of queue is like top of the stack and front is at the bottom of the stack.
So when you are the doing the dequeue, you have no access to the bottom element.

5. Write a C function for question #3 to show the enqueue function. Remember this function
must use a stack operation. This function must have the following:
 A void return data type and accepts nothing
 Prompts the user to enter an integer value to be enqueued
 A loop to continuously prompt the user to enter values
 Keep a count of the number of elements enqueued.
 User can stop entering values when a sentinel value -999 is entered.
 Use a function isFullStack() which returns true if it is full otherwise returns false
 Use a function pushInStack(int value) which pushes the element in the stack.

You might also like