Inserting at index 0 of an array holding n elements costs:
Arrays, Linked Lists and Array Lists
Arrays (the scoreboard example, inserting and removing by shifting), singly linked lists (insert/remove at head and tail), doubly linked lists with header and trailer sentinels, array lists, growable arrays (incremental vs. doubling, amortized analysis), positional lists and iterators. The Midterm 1 and Quiz 1 question banks are mostly linked-list code. (This handout carries an earlier student’s handwritten notes.)
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 34 slides
- 1 9/17/2016 5:37 PM
- 2 9/17/2016 5:37 PM
- 3 9/17/2016 5:37 PM
- 4 9/17/2016 5:37 PM
- 5 9/17/2016 5:37 PM
- 6 Slide 6
- 7 9/17/2016 5:37 PM
- 8 9/17/2016 5:37 PM
- 9 9/17/2016 5:37 PM
- 10 9/17/2016 5:37 PM
- 11 9/17/2016 5:37 PM
- 12 b c
- 13 9/17/2016 5:37 PM
- 14 y a
- 15 9/17/2016 5:37 PM
- 16 traile 9/17/2016 5:37 PM
- 17 AT.n.ie
- 18 Slide 18
- 19 9/17/2016 5:37 PM
- 20 sic
- 21 9/17/2016 5:37 PM
- 22 about.mn
- 23 9/17/2016 5:37 PM
- 24 9/17/2016 5:37 PM
- 25 9/17/2016 5:37 PM
- 26 9/17/2016 5:37 PM
- 27 9/17/2016 5:37 PM
- 28 9/17/2016 5:37 PM
- 29 9/17/2016 5:37 PM
- 30 9/17/2016 5:37 PM
- 31 9/17/2016 5:37 PM
- 32 9/17/2016 5:37 PM
- 33 9/17/2016 5:37 PM
- 34 9/17/2016 5:37 PM
9/17/2016 5:37 PM
Notes
Arrays: length/capacity, declaring, the GameEntry/Scoreboard example.
9/17/2016 5:37 PM
Notes
Adding an entry at index i shifts n − i elements forward; removing shifts them back.
Exam angle
9/17/2016 5:37 PM
Notes
Singly linked list: nodes with element + next; head (and tail) references.
9/17/2016 5:37 PM
Notes
Insert at head: new node → newest.next = head → head = newest. O(1).
Exam angle
code to insert at the head
Correct code to insert node N at the head of a doubly linked list:
- Quiz 1.
9/17/2016 5:37 PM
Notes
Remove at head: head = head.next. O(1). Remove at tail of a SINGLY linked list is NOT O(1): you must walk to the node before the tail.
Exam angle
remove the last node / second-to-last
which operation is slowest in an SLL
Which singly-linked-list operation takes O(n) even with a tail pointer?
b c
Notes
Doubly linked list: prev and next; header and trailer sentinels simplify the edge cases.
9/17/2016 5:37 PM
Notes
Insert between p and its successor: link the new node both ways (4 pointer updates). Delete p: p.prev.next = p.next; p.next.prev = p.prev.
Exam angle
Before.next = After; After.prev = Before
To unlink node X between Before and After in a DLL:
- Quiz 1.
A doubly linked list beats a singly linked list for:
- Quiz 1.
AT.n.ie
Notes
Array list (java.util.List): get, set, add(i, e), remove(i); add/remove at i shift elements → O(n).
sic
Notes
Growable array: when full, copy into a bigger array.
9/17/2016 5:37 PM
Notes
Incremental strategy (+c each time): n pushes cost O(n²) → amortized O(n) per push. Doubling: O(n) total → amortized O(1).
Memory trick Grow by DOUBLING, not by a constant.
Exam angle
With the doubling strategy, the amortized cost of a push is:
about.mn
Notes
Positional list ADT: positions stay valid while other elements change.
9/17/2016 5:37 PM
Notes
Iterator: hasNext(), next(). Iterable: iterator(). for-each uses them.