Data Structures (IB CS B2.2): Arrays, Lists, Stacks and Queues

"IB CS B2.2 explained: static vs dynamic structures, arrays and lists, and stacks (LIFO) and queues (FIFO) with full push/pop and enqueue/dequeue trace tables and exam tips.

Imagine trying to run a library where every book is just thrown into one giant pile. Finding anything would be a nightmare. Data structures are how a program keeps its information tidy, so it can store, find, and update values quickly. In B2.2 you need to know a handful of these structures, what makes each one special, and when you would reach for each. Stacks and queues are the ones exam questions love to trace, so we will work those step by step. Let's make them click.

What is a data structure?

A data structure is simply an organised way of holding data in memory so a program can work with it efficiently. The "right" structure depends on what you need to do: keep things in order, look items up by position, always grab the most recent thing, or always serve the oldest thing first. Choosing well makes your code faster and simpler. Choosing badly makes it slow and awkward.

A useful first split is static versus dynamic.

A static structure has a fixed size, decided when it is created. Think of a row of theatre seats: the number is set, and you cannot add a seat halfway through the show. A dynamic structure can grow and shrink while the program runs, like a queue at a coffee shop that gets longer and shorter all day.

What is an array?

An array is a collection of items stored in order, where each item sits at a numbered position called an index. The crucial detail for exams: indexing almost always starts at 0. So in an array of five items, the valid indices are 0, 1, 2, 3, and 4. There is no index 5.

The big advantage of an array is fast access. If you know the index, the program jumps straight to that item without checking everything before it. The trade-off is that a classic array is static, so its size is fixed when you create it.

A two-dimensional array is just a grid, like a spreadsheet. You reach a cell with two indices: one for the row and one for the column, written as array[row][col]. This is perfect for things like a noughts-and-crosses board or a table of marks.

Quick array check

A grid is read grid[row][col], and both start at 0. For this grid:

grid = [ [5, 12, 7],
         [9,  2, 18]

grid = [ [5, 12, 7],
         [9,  2, 18]

grid = [ [5, 12, 7],
         [9,  2, 18]

grid[1][2] is row 1, column 2, which is 18. grid[0][0] is 5. And grid[2][0]? There is no row 2 (rows are 0 and 1), so that is an out-of-bounds error and the program would crash. Off-by-one slips like this are the most common array mistake in the exam.

What is a list, and how is it different from an array?

A list is also an ordered collection, but it is usually dynamic: it can grow and shrink as the program runs. You can append new items to the end, insert items in the middle, and remove items, and the list resizes itself.

So the headline difference is flexibility. An array is typically fixed in size and very fast to access by index. A list trades a little of that raw speed for the freedom to change size whenever you need to. In many languages the everyday "list" type does both jobs, but for IB you should be able to state the distinction clearly.

What is a stack? (LIFO)

A stack follows one strict rule: LIFO, which stands for Last In, First Out. The last item you put on is the first item you take off. Picture a stack of plates: you add a clean plate to the top, and when you need one, you take it from the top too. You never pull a plate from the middle.

A stack's operations all work at the top: push adds an item to the top, pop removes the top item and returns it, peek (or top) looks at the top item without removing it, and isEmpty checks whether there is anything there. Stacks show up everywhere: the undo button in an editor, the back button in a browser, and the way a computer keeps track of function calls (the call stack).

Worked example: tracing a stack

Carry out these operations on an empty stack, in order: push(6), push(1), push(9), pop(), push(4). The trick examiners want is a trace table showing the contents after each step.

Operation

Stack (bottom → top)

Returned

push(6)

[6]

-

push(1)

[6, 1]

-

push(9)

[6, 1, 9]

-

pop()

[6, 1]

9

push(4)

[6, 1, 4]

-

Read down the table. Each push adds to the right-hand (top) end. The pop() removes the most recent item, 9, because a stack is LIFO, and returns it. The final push(4) lands on top, so the stack ends as [6, 1, 4] with 4 on top. If a question asks "what does pop() return?", the answer is always the item that was pushed most recently.

What is a queue? (FIFO)

A queue follows the opposite rule: FIFO, which stands for First In, First Out. The first item to join is the first item to leave, exactly like a fair line of people. You join at the rear and you are served from the front.

Its operations mirror the stack's but at opposite ends: enqueue adds an item to the rear, dequeue removes the item from the front and returns it, front views the front item without removing it, and isEmpty checks whether the queue is empty. Queues are used wherever order of arrival matters, such as print jobs waiting for a printer or tasks waiting for a processor.

Worked example: tracing a queue

Now the same idea for a queue. Start empty and carry out: enqueue("A"), enqueue("B"), enqueue("C"), dequeue(), enqueue("D").

Operation

Queue (front → rear)

Returned

enqueue("A")

[A]

-

enqueue("B")

[A, B]

-

enqueue("C")

[A, B, C]

-

dequeue()

[B, C]

A

enqueue("D")

[B, C, D]

-

Each enqueue joins at the rear (right). The dequeue() removes from the front, so it returns A, the item that had been waiting longest, because a queue is FIFO. The queue ends as [B, C, D]. Notice the contrast with the stack: same five operations, but the stack gave back the newest item and the queue gave back the oldest.

Your turn (guided practice)

Try this before reading the answer. Starting from an empty stack, trace: push(3), push(7), pop(), push(5), pop(). What does each pop() return, and what is left on the stack?

Work it line by line: push(3) gives [3]; push(7) gives [3, 7]; the first pop() returns 7 and leaves [3]; push(5) gives [3, 5]; the second pop() returns 5 and leaves [3]. So the pops return 7 then 5, and the stack finishes as [3]. If you got that, you have the LIFO rule locked in.

When should you use a stack or a queue?

Match the structure to the behaviour the problem needs. A program that lets a user undo their last action needs the most recent action back first, which is LIFO, so a stack fits. A program that prints documents in the order they were sent needs the oldest first, which is FIFO, so a queue fits. Picking a queue for undo (or a stack for a print spooler) would serve things in the wrong order, and that mismatch is exactly what exam questions test.

Common exam mistakes

A frequent slip is forgetting that indices start at 0. If an array holds five items, the last one is at index 4, not 5. Reaching for index 5 is an "out of bounds" error.

Students also mix up LIFO and FIFO. A quick anchor: a stack is like a stack of plates (top only), while a queue is like a queue of people (back in, front out). Tie each acronym to its everyday picture and they stop blurring together.

Another is muddling the operation names. Stacks use push and pop. Queues use enqueue and dequeue. Using "add" and "remove" loosely can cost you marks when the question wants the precise term.

When tracing, say what each operation returns, not just the final contents. A pop() or dequeue() returns a value, and questions often award a mark for that value specifically.

Finally, watch the static versus dynamic distinction. Saying an array can simply grow to any size is risky, because a classic array is fixed in size. If you need something that resizes freely, that is a list or another dynamic structure.

Quick recap

A data structure is an organised way of storing data so a program can use it efficiently. Static structures have a fixed size; dynamic structures can grow and shrink at runtime. An array stores items by index starting at 0; a 2D array is a grid reached by array[row][col]. A stack is LIFO: push and pop (and peek) at the top; a pop returns the most recently pushed item. A queue is FIFO: enqueue at the rear, dequeue at the front; a dequeue returns the item that waited longest.

Frequently asked questions

What is the difference between an array and a list? An array is an ordered collection accessed by index, and a classic array has a fixed size. A list is also ordered but is dynamic, so it can grow and shrink as the program runs. Arrays favour fast, fixed-size access; lists favour flexibility.

Why do array indices start at 0? The index represents an offset from the start of the array, so the first item sits at offset 0. This means an array of n items has valid indices from 0 to n minus 1, and there is no index equal to the length.

What does LIFO mean? LIFO stands for Last In, First Out. It describes a stack, where the last item pushed on is the first item popped off, just like taking the top plate from a pile.

What does FIFO mean? FIFO stands for First In, First Out. It describes a queue, where the first item to join is the first to leave, just like the front of a line being served first.

When should I use a stack instead of a queue? Use a stack when you need the most recent item first, such as an undo feature or a browser back button. Use a queue when you need to handle items in their order of arrival, such as print jobs or tasks waiting in turn.

Is an array static or dynamic? A classic array is static, meaning its size is fixed when it is created. Lists, stacks, and queues are usually implemented as dynamic structures that can change size while the program runs.

Looking for a printable summary? Grab the Data Structures (IB CS B2.2): Arrays, Lists, Stacks and Queues revision sheet, a three-page knowledge organiser covering everything above.

Looking for an IB Computer Science tutor?

Hi, I'm Yuness, the tutor behind Shuttle Learning. I work one to one with IB Computer Science students at SL and HL, and I deliberately take on only a handful each year so every student gets my full attention. Most go on to earn the 6s and 7s they were aiming for, in the final exams and the IA alike.

If you would like that kind of support, book a free 15-minute call and tell me what you are stuck on. You can press BOOK A LESSON .

 

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com