CS 46B - Lecture 19

Cover page image

Pre-class reading

Implementing Stacks and Queues

Stacks as Linked Lists

Lecture 19 Clicker Question 1

Where should the nodes be added/removed for maximum efficiency?

  1. push adds before first, pop removes last
  2. push adds before first, pop removes first
  3. push adds after last, pop removes last
  4. push adds after last, pop removes first

Stacks as Array Lists

Lecture 19 Clicker Question 2

Where should the elements be added/removed for maximum efficiency?

  1. push adds before first, pop removes last
  2. push adds before first, pop removes first
  3. push adds after last, pop removes last
  4. push adds after last, pop removes first

Queues as Linked Lists

Lecture 19 Clicker Question 3

Where should the nodes be added/removed for maximum efficiency?

  1. add adds before first, remove removes last
  2. add adds before first, remove removes first
  3. add adds after last, remove removes last
  4. add adds after last, remove removes first

Queues as Circular Arrays

Lecture 19 Clicker Question 4

Assume the buffer has size 10. What is its contents after these operations?

for (i = 1; i <= 8; i++) q.add(i);
for (i = 1; i <= 4; i++) q.remove();
for (i = 1; i <= 8; i++) q.add(i);
for (i = 1; i <= 4; i++) q.remove();
  1. 1 2 3 4 5 6 7 8
  2. 3 4 5 6 7 8 7 8 1 2
  3. 3 4 5 6 7 8 0 0 1 2
  4. Something else