IBDP Computer Science B2.2 Data structures HL Paper 1 - New Syllabus

Question

(a) List $3$ features of a dynamic data structure. $[3]$
(b)
(i) Describe the structure of a circular linked list. $[3]$
(ii) State $2$ applications of circular linked lists in computing. $[2]$
(c) Stacks and queues are linear data structures.
(i) Identify $2$ differences between stack data structures and queue data structures. $[2]$
The stack CUSTOMERS holds the names of customers.
An algorithm is needed to remove names from the stack CUSTOMERS and store them in $2$ queues.
Customers’ names beginning with the letters A to M (inclusive) should be stored in queue ONE, and customers’ names beginning with the letters N to Z (inclusive) should be stored in queue TWO.
For example, the stack CUSTOMERS holds the following names:
White, Anna← top
Brown, Matt 
Grey, Tom 
Gold, Emma 
Silver, Carla 
After execution of the algorithm:
• the stack CUSTOMERS should be empty;
• queue ONE should contain, from front to rear: Brown, Matt → Grey, Tom → Gold, Emma;
• queue TWO should contain, from front to rear: White, Anna → Silver, Carla.
You may assume that:
• the subprogram firstletter(S) is available. It accepts the string S and returns the first character in the string S;
• the stack CUSTOMERS already contains names, and queue ONE and queue TWO have been initialized.

(ii) Construct an algorithm in pseudocode to remove names from the stack CUSTOMERS and store them in $2$ queues, as described.

You must use stack access methods and queue access methods in your response. $[5]$

Most-appropriate topic codes (CED):

TOPIC B2.2: Data structures — parts (a), (c)(i) and (c)(ii)
TOPIC B4.1: Fundamentals of ADTs — parts (b)(i) and (b)(ii)
▶️ Answer/Explanation
Detailed solution

(a)
Three features of a dynamic data structure are:

• Its size is not fixed in advance and can change while the program is running.
• Memory is allocated dynamically as elements are added, reducing unnecessary reserved memory.
• Elements can generally be inserted or deleted more flexibly than in a fixed-size structure.

Dynamic data structures are especially useful when the amount of data that will be stored is not known before the program starts.

Answer: Variable size, dynamic memory allocation and flexible insertion/deletion.

(b)(i)
A circular linked list consists of nodes. Each node contains a data field and a pointer/reference to the next node.

The last node does not point to null. Instead, it points back to the first/head node, forming a continuous circular structure.

$\text{Node}_1 \rightarrow \text{Node}_2 \rightarrow \text{Node}_3 \rightarrow \cdots \rightarrow \text{Node}_1$

An external pointer can be used to identify the first node in the list.

Answer: Each node stores data and a pointer to the next node, while the final node points back to the first node.

(b)(ii)
Two applications of circular linked lists are:

Round-robin scheduling: processes can be handled repeatedly in a continuous cycle.
Media playlists: after the last item is reached, processing can return to the first item automatically.

Other acceptable examples include multiplayer turn systems, circular queues, memory management and repeated navigation systems.

Answer: Round-robin scheduling and repeating playlists.

(c)(i)
A stack follows the last in, first out (LIFO) principle, meaning the most recently inserted item is removed first.

A queue follows the first in, first out (FIFO) principle, meaning the item inserted earliest is removed first.

There is also a difference in the access points:

• A stack inserts and removes items at the top, using operations such as push() and pop().
• A queue inserts items at the rear using enqueue() and removes items from the front using dequeue().

Answer: A stack is LIFO and inserts/removes at the same end, whereas a queue is FIFO and inserts at the rear but removes from the front.

(c)(ii)
The algorithm repeatedly removes the top customer from the stack. The first letter of the name is checked and the customer is then added to the appropriate queue.

while not CUSTOMERS.isEmpty()
X = CUSTOMERS.pop()
if firstletter(X) >= ‘A’ and firstletter(X) <= ‘M’
then ONE.enqueue(X)
else TWO.enqueue(X)
end if
end loop

The loop continues until CUSTOMERS.isEmpty() is true.

For every iteration:

CUSTOMERS.pop() removes the item currently at the top of the stack.
firstletter(X) obtains the first character of the customer’s name.
• If the first letter is between ‘A’ and ‘M’ inclusive, ONE.enqueue(X) places the name at the rear of queue ONE.
• Otherwise, TWO.enqueue(X) places the name at the rear of queue TWO.

Because the names are popped from the stack in top-to-bottom order and enqueued at the rear of each queue, the required order of the names is preserved.

Scroll to Top