All chapters[•]Data StructuresChapter 03
Data Structures · Chapter 3

Linear Data StructuresData Structures · Chapter 3

Three ways of arranging items in a line, each defined by which items you are allowed to reach: a stack gives back the most recent one, a queue the oldest, a deque either end. The restriction is a promise, and the promise is what lets every operation take constant time. We build each one twice, in an array and in a chain of nodes, and then use them on two classic problems: checking brackets and visiting a network nearest-first.

Instructors: Boxi Yu · Yintong Huo · Python and Java versions (switch at the top) · Every program here has been run, and every output shown is the real output. · All chapters

Which end?

The same row of five items (A…E) is handed out three ways: a stack returns E, D, C, B, A; a queue returns A, B, C, D, E; a deque lets you choose the end at every step. The order of the returned items is printed under each.

Why this matters

about 90–120 minutes (4 videos of about 8 minutes plus the exercises).

A smaller promise is easier to keep

Because a stack only ever touches its top, push and pop take constant time however many items it holds; restricting access is a design decision, not a weakness.

Arrays and chains are two answers to the same question

Every structure here can be built on a growing array or on linked nodes, and the trade (copying now and then versus a pointer per item) comes back in every later chapter.

These three show up inside everything else

The call stack that runs your recursion, the queue behind breadth-first search, the deque behind sliding windows: you will meet them again in Chapters 4 and 9.

5 parts

Four videos with their notes, then the summary and practice. Read them in order.

01

Stacks

A stack lets you add an item on top, look at the top and remove the top, nothing else: last in, first out. That is exactly what Back and Undo need. We run a small example, see why the restriction makes every operation O(1), and build a stack twice: in an array that doubles when full and in a chain of nodes.

LIFOpush / pop / toparray stacklinked stackdoubling
Open →
02

Queues: circular array and linked

A queue hands items back in the order they arrived: first in, first out. Storing it in an array with the front at index 0 forces a shift on every dequeue, so we move the front instead and bend the array into a circle; the remainder operator does the wrapping. The alternative is a chain with a head and a tail.

FIFOenqueue / dequeuecircular arrayfront and sizelinked queuehead and tail
Open →
03

Deques

A deque (double-ended queue) adds and removes at both ends and contains the stack and the queue as special cases. A circular array gives all four end operations in constant time because the front can also step backwards; a singly linked chain cannot remove at the tail quickly, so a linked deque needs two links per node. A palindrome check shows what it is for.

dequeadd_first / add_lastremove_first / remove_lastfront steps backwardsdoubly linked
Open →
04

Applications: bracket matching and a BFS preview

A stack matches each closing bracket with the most recent unclosed opener; counting brackets is not enough. A queue visits a network nearest-first: breadth-first search. Both run in one pass with constant work per item, so O(n) and O(n + m).

bracket matchingstackthree kinds of failurebreadth-first searchqueueseen set
Open →
05

Summary and practice

A one-page map of the chapter, five self-check questions, ten exercises with solutions, and a way to test claims an AI makes about complexity.

self-checkexercises
Open →

How to use these notes

Watch · Predict · Step through · Practise

STEP 1

Watch

Each part starts with its video, 8–10 minutes, in Python or Java.

STEP 2

Predict

Answer the amber Predict box before you open the answer.

STEP 3

Step through

The interactive demos are being added; each one is described where it will appear.

STEP 4

Practise

Finish with the self-check and the exercises, with templates in your language.