ISF DP Computer Science

Stacks and Queues #

This page contains information and coding exercises for queues.

💻 Open up the pseudocode compiler in a new tab. You will be using this website to complete the exercises below.


Creating a new Queue #

NAMES = new Queue()

Queue methods #

NAMES.enqueue("Bob") // put an item into the end of a queue
NEXT_NAME = NAMES.dequeue() // remove and return the item from the front of the Queue
NAMES.isEmpty() // returns TRUE if queue is empty, FALSE otherwise

Creating a new Stack #

NAMES = new Stack()

Stack methods #

NAMES.push("Bob") // adds an item to the top of the stack
NAMES.pop() // remove and return the item from the front of the top of the stack
NAMES.isEmpty() // returns TRUE if stack does not contain any elements, FALSE otherwise

Practice Exercises #

Exercise 1: Reverse an Queue, using a Stack #

💻 Write a program that does the following:
  1. Create a Queue
  2. Add 5 items to it
  3. Create a Stack for temporary storage
  4. Loop through the Queue, and push each item onto the Stack
  5. Loop through the Stack, and push all items back onto the Queue

At the end, the Queue should be reversed. For example:

This Queue:

- back -
Abby
Brittany
Chloe
Daria
Eloise
- front -

Should become:

- back -
Eloise
Daria
Chloe
Brittany
Abby
- front -

Exercise 2: Reverse a Stack, using a Queue #

💻 Similarly to the exercise above, you should write pseudocode that reverses the order of a Stack, using a Queue as temporary storage

This Stack:

- top -
Abby
Brittany
Chloe
Daria
Eloise
- bottom -

Should become:

- top -
Eloise
Daria
Chloe
Brittany
Abby
- bottom -

✏️ When you’re done, paste your finished pseudocode in your lab log.