Stacks, Queues, and Deques

15. Stacks, Queues, and Deques#

Undoing the latest edit and serving the earliest waiting request require different access rules. This chapter connects stack, queue, and deque contracts to array and linked representations, bounded capacity, and ring-buffer wraparound. You will apply the structures to histories, delimiter checks, and worklists while checking both failure behavior and operation costs.

  • Stack: A stack uses last-in, first-out behavior for undo, nested processing, and backtracking.

  • Queue: A queue uses first-in, first-out behavior for service lines and scheduled work.

  • Deque: A deque supports insertion and removal at both ends, making it a flexible boundary structure.

  • Implementation choice: Array-based and linked implementations can provide the same ADT operations with different costs.

  • Application fit: Restricted-access structures are useful when the rule for who comes next is part of the problem.

Chapter Video

C# Data Structures - Arrays, Lists, Dictionaries, HashSets, Stacks, Queues (3:34) by Mark Inman.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Specify stack, queue, and deque access rules and failure behavior.

  2. Trace count, capacity, stack top, and ring-buffer wraparound.

  3. Implement generic array stacks, bounded ring queues, and linked queues while preserving their invariants.

  4. Distinguish worst-case and amortized operation costs and compare representation tradeoffs.

  5. Apply and test delimiter matching, undo/redo histories, queue worklists, and scheduling policies.

Chapter Flow

Glossary

#

Term

Explanation

1

Stack

A collection where the most recently added item is the first removed.

2

Queue

A collection where the earliest added item is the first removed.

3

Deque

A double-ended queue that supports adding and removing at both ends.

4

LIFO

Last in, first out; the access rule used by stacks.

5

FIFO

First in, first out; the access rule used by queues.

6

Push

Add an item to the top of a stack.

7

Pop

Remove and return the top item from a stack.

8

Peek

Look at the next item without removing it.

9

Enqueue

Add an item to the back of a queue.

10

Dequeue

Remove and return the front item from a queue.

11

Front

The next queue item to be removed.

12

Back

The end where new queue items are normally added.

13

Ring buffer

An array-based queue representation that wraps indexes around a fixed array.

14

Overflow

A condition where a bounded structure has no space for another item.

15

Underflow

A condition where code tries to remove an item from an empty structure.

16

Backtracking

A strategy that uses a stack to remember choices that may need to be undone.

17

Worklist

A collection of pending tasks or states to process.

18

Breadth-first order

Processing nearest or earliest-discovered items before later layers, often with a queue.

19

Invariant

A representation rule that holds after every completed operation.

20

Amortized cost

Total cost over an operation sequence divided by its length.

21

Snapshot

A saved state that can be restored, such as a prior report version.

22

Starvation

A waiting task keeps being bypassed by other work under a scheduling policy.

23

Admission policy

The specified response when incoming work cannot be accepted, separate from removal order.

Lecture Slides

Chapter 15 Slides