Practice Flashcards
What is the difference between a static and a dynamic data structure?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All Flashcards in Topic 6.2
Below are all 20 flashcards for this topic. Sign up free to track your progress and get personalized review schedules.
6.2.15 cards
What is the difference between a static and a dynamic data structure?
A static structure has its size fixed when it is created, in one contiguous block of memory. A dynamic structure grows and shrinks while the program runs, taking more memory only when it needs it.
Why is reading a position in a static array fast?
Everything sits in one block with items of equal size, so the address of item 20 is simply the start plus twenty item-widths. It is one calculation, with nothing searched for — O(1).
What does a dynamic structure cost when it grows?
When it outgrows its block it must claim a bigger one and copy everything across. That single operation is expensive, but it happens rarely, so the average cost of adding an item stays low.
When should you choose a static structure?
When the size is genuinely fixed — twelve months, seven days, sixty-four squares — or when a very large amount of data is scanned repeatedly and both speed and predictable memory matter.
Why is a fixed-size array wrong for an unknown number of items?
You must guess a maximum. Guess too small and it overflows or silently discards data; guess too large and most of the memory is never used — with still no guarantee the guess was enough.
6.2.25 cards
How are items in a list addressed?
By position, starting at 0. A list of four items has positions 0, 1, 2 and 3, so asking for position 4 is an error — the commonest list bug there is.
How do you add to and remove from a dynamic list?
append adds at the end, insert(position, value) adds anywhere and shifts the rest along. remove(value) takes out the first match, and pop() removes the last item and gives it back.
Why must you not remove items while looping over a list?
Removing shifts everything after it down one place while the loop counter moves up, so items get skipped silently. Build a new list instead, or loop over a copy.
What is a 2D list and how is it addressed?
A list whose items are themselves lists — a grid. It is addressed [row][column], always in that order: grid[1][2] is row 1, column 2.
Why does visiting every cell of a grid need a nested loop?
The outer loop gives you each row, which is itself a list; the inner loop reaches the individual values inside it. That is also why scanning a grid is O(rows × columns).
6.2.35 cards
What is a stack?
A structure that allows adding and removing at one end only, the top. The last item put in is the first taken out — last in, first out, or LIFO.
What are the four stack operations?
push adds an item on top; pop removes the top item and returns it; peek returns the top item without removing it; isEmpty says whether there is anything in the stack at all.
Why are all stack operations O(1)?
Every operation touches only the top. Nothing is shifted along and nothing is searched for, so the cost is the same whether the stack holds three items or three million.
Give three uses of a stack.
Undo, where the most recent action must be reversed first; the call stack, since function calls unwind in reverse order; and checking that brackets match, because the most recent opening bracket must close first.
What causes a stack overflow?
A stack that keeps growing with nothing being popped until it runs out of memory — usually a function that calls itself with no way to stop.
6.2.45 cards
What is a queue?
A structure added to at one end and taken from at the other: items join at the back and leave from the front. The first item in is the first out — first in, first out, or FIFO.
What are the four queue operations?
enqueue adds an item at the back; dequeue removes the item at the front and returns it; front returns the next item out without removing it; isEmpty says whether anyone is waiting.
Why is a plain list a poor implementation of a queue?
Removing from the front of a list shifts every remaining item down one place, which is O(n) every time. A proper queue structure removes from the front in constant time.
What one question chooses between a stack and a queue?
Does the most recent item matter most, or the one that has waited longest? Most recent means a stack; longest waiting means a queue.
Give three uses of a queue.
Print jobs, so they print in the order sent; process scheduling, where round robin takes the front process and returns it to the back; and buffering data such as keystrokes or network packets, which must be handled in arrival order.
Topic 6.2 study notes
Full notes & explanations for Data structures
Computer Science exam skills
Paper structures, command terms & tips
Want smart review reminders?
Sign up free to track your progress. Our spaced repetition algorithm will tell you exactly which cards to review and when.
Start Free