Skip to content
Ameen Abozaid
BEG-040

Linear data structures

  • 7 Lessons
  • 73 min of reading
  • 5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Arrays
  • dynamic arrays
  • linked lists
  • stacks
  • queues

Learn first

73 min of reading · 5 h of practice (estimate)

Learning outcomes

  1. Implement a dynamic array and explain amortized O(1) append.
  2. Implement a singly linked list with insert, remove, and search.
  3. Implement a stack and a queue and pick one for a given problem.
  4. Compare array-based and linked structures by operation cost.

Lessons

  1. 01Arrays in memory12 min read
  2. 02Dynamic arrays and resizing10 min read
  3. 03Linked lists11 min read
  4. 04Stacks9 min read
  5. 05Queues10 min read
  6. 06Choosing: ArrayList vs LinkedList vs ArrayDeque11 min read
  7. 07Demo: building MyArrayList and watching it resize10 min read

Lab and exercises

Lab

BEG-040-LAB1 · Build your own

Outcomes: 1–4 · Time: ~5 h

  1. Implement MyArrayList<T> (add, get, remove(int), size) with doubling growth.
  2. Implement MyLinkedList<T> (addFirst, addLast, insert, remove, contains).
  3. Implement a stack and a queue (any base) and write a bracket-matching checker with your stack.
  4. Write the Big O of each method in a comment above it.

Acceptance: passing tests (plain main checks are fine until BEG-045); complexity of each method documented.

Exercises (assessment: implementation plus 4 problems)

IDOutcomeTask
BEG-040-EX011A dynamic array has capacity 4 and doubles when full. Count the copies done while adding 17 items, starting empty.
BEG-040-EX021Explain in three sentences why append is amortized O(1) and not worst-case O(1).
BEG-040-EX032Draw the nodes before and after removing the middle item of A -> B -> C; write the one line that rewires.
BEG-040-EX043Use a stack to check whether "([)]" and "{[()]}" are balanced.
BEG-040-EX053Choose stack or queue: undo history; print jobs; browser back button; customers at a counter.
BEG-040-EX064Fill in the cost (O(1) or O(n)) for ArrayList and a singly linked list: get by index, add at end, add at front, remove at front.
Show solutions
  • EX01 Resizes happen when adding items 5, 9, and 17 (to capacities 8, 16, 32): 3 copies, moving 4 + 8 + 16 = 28 items in total.
  • EX02 Most appends write one slot. Occasionally one append copies all items (O(n)). The total over n appends is about 2n, so on average each is O(1), but a single append can be O(n).
  • EX03 Before: A -> B -> C. After: A -> C. a.next = b.next; (with a the node before b).
  • EX04 "([)]" is not balanced () meets [ on top); "{[()]}" is balanced and the stack ends empty.
  • EX05 Stack; queue; stack; queue.
  • EX06 ArrayList: O(1), O(1) amortized, O(n), O(n). Singly linked list: O(n), O(n) without a tail reference (O(1) with one), O(1), O(1).

Quiz

Outcomes: O1 dynamic array · O2 linked list · O3 stack and queue · O4 compare structures.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Array access a[i] costs:O1
  2. 2A dynamic array that is full usually:O1
  3. 3Growing by doubling makes append:O1
  4. 4Growing by +1 each time makes n appends cost about:O1
  5. 5A singly linked list node holds:O2
  6. 6Searching a linked list by value is:O2
  7. 7The last node's next is:O2
  8. 8A stack is:O3
  9. 9Which fits "tasks processed in arrival order"?O3
  10. 10Which JDK class is the usual choice for a stack or queue?O4

Practical part

Implementation of the lab (MyArrayList, MyLinkedList, stack, queue, bracket checker) plus 4 problems (BEG-040-EX01, EX04, EX05, EX06). Pass: tests pass, complexity documented, and at least 3 of the 4 problems correct. Exercises EX02 and EX03 are also part of the module.

Common mistakes

Losing the list's tail on removal; resizing by +1 instead of doubling.

Assessment

Implementation plus 4 problems.

Career relevance

Queues reappear as message queues (MID-026).

Type to search levels, phases, modules and lessons.