Question 124 marks
Question
An unordered linked list uses a 1D array to store the data. Each item in the linked list is of a record type, node, with a field data and a field nextNode. The current contents of the linked list are: startPointer 0 Index data nextNode 0 1 1 emptyList 5 1 5 4 2 6 7 3 7 -1 4 2 2 5 0 6 6 0 8 7 56 3 8 0 9 9 0 -1 (a) The following is pseudocode for the record type node. TYPE node DECLARE data : INTEGER DECLARE nextNode : INTEGER ENDTYPE Write program code to declare the record type node. Save your program as question1. Copy and paste the program code into part 1(a) in the evidence document. [2] (b) Write program code for the main program. Declare a 1D array of type node with the identifier linkedList, and initialise it with the data shown in the table on page 2. Declare the pointers. Save your program. Copy and paste the program code into part 1(b) in the evidence document. [4] (c) The procedure outputNodes() takes the array and startPointer as parameters. The procedure outputs the data from the linked list by following the nextNode values. (i) Write program code for the procedure outputNodes(). Save your program. Copy and paste the program code into part 1(c)(i) in the evidence document. [6] (ii) Edit the main program to call the procedure outputNodes(). Take a screenshot to show the output of the procedure outputNodes(). Save your program. Copy and paste the screenshot into part 1(c)(ii) in the evidence document. [1] (d) The function, addNode(), takes the linked list and pointers as parameters, then takes as input the data to be added to the end of the linkedList. The function adds the node in the next available space, updates the pointers and returns True. If there are no empty nodes, it returns False. (i) Write program code for the function addNode(). Save your program. Copy and paste the program code into part 1(d)(i) in the evidence document. [7] (ii) Edit the main program to: • call addNode() • output an appropriate message depending on the result returned from addNode() • call outputNodes() twice; once before calling addNode() and once after calling addNode(). Save your program. Copy and paste the program code into part 1(d)(ii) in the evidence document. [3] (iii) Test your program by inputting the data value 5 and take a screenshot to show the output. Save your program. Copy and paste the screenshot into part 1(d)(iii) in the evidence document. [1]
Mark scheme
1(a) 1 mark per bullet point 2 • Declaring record/class with name node… • …declaring data and next node (both as Integers) Example code: Visual Basic Structure node Dim Data As Integer Dim nextNode As Integer End Structure Python class node: def __init__(self, theData, nextNodeNumber): self. Data = theData self.nextNode = nextNodeNumber Java class node{ private Integer Data; private Integer nextNode; public node(Integer dataP, Integer nextNodeP){ this.Data = dataP; this.nextNode = nextNodeP; } } 1(b) 1 mark per bullet point 4 • Declaring array named linkedList with data type node • Assigning all nodes correctly as record/object nodes … • …with correct values stored • declaring startPointer as 0, emptyList as 5 Example code: Visual Basic Dim linkedList(9) As node linkedList(0).data = 1 linkedList(0).nextNode = 1 linkedList(1).data = 5 linkedList(1).nextNode = 4 linkedList(2).data = 6 linkedList(2).nextNode = 7 linkedList(3).data = 7 linkedList(3).nextNode = -1 linkedList(4).data = 2 linkedList(4).nextNode = 2 linkedList(5).data = 0 linkedList(5).nextNode = 6 linkedList(6).data = 0 linkedList(6).nextNode = 8 linkedList(7).data = 56 linkedList(7).nextNode = 3 linkedList(8).data = 0 linkedList(8).nextNode = 9 linkedList(9).data = 0 linkedList(9).nextNode = -1 Dim startPointer As Integer = 0 Dim emptyList As Integer = 5 1(b) Python linkedList = [node(1,1),node(5,4),node(6,7),node(7,-1),node(2,2),node(0,6), node(0,8),node(56,3),node(0,9),node(0,-1)] startPointer = 0 emptyList = 5 Java public static void main(String[] args){ node[] linkedList = new node[10]; linkedList[0] = new node(1,1); linkedList[1] = new node(5, 4); linkedList[2] = new node(6, 7); linkedList[3] = new node(7,-1); linkedList[4] = new node(2,2); linkedList[5] = new node(0,6); linkedList[6] = new node(0,8); linkedList[7] = new node(56, 3); linkedList[8] = new node(0,9); linkedList[9] = new node(0,-1); Integer startPointer = 0; Integer emptyList = 5; } 1(c)(i) 1 mark per bullet point 6 • Procedure outputNodes … • …taking linked list and start pointer as parameters • Looping until nextNode/pointer is –1 • Outputting the node data in the correct order, i.e. following pointers • Updating pointer to current node’s nextNode • Using the correct record/class field/properties throughout Example code: Visual Basic Sub outputNodes(ByRef linkedList, ByVal currentPointer) While (currentPointer <> -1) Console.WriteLine(linkedList(currentPointer).data) currentPointer = linkedList(currentPointer).nextNode End While End Sub Python def outputNodes(linkedList, currentPointer): while(currentPointer != -1): print(str(linkedList[currentPointer].data)) currentPointer = linkedList[currentPointer].nextNode Java public static void outputNodes(node[] linkedList, Integer currentPointer){ while(currentPointer != -1){ System.out.println(linkedList[currentPointer].data); currentPointer = linkedList[currentPointer].nextNode; } } 1(c)(ii) Screenshot showing: 1 1 5 2 6 56 7 1(d)(i) 1 mark per bullet point to max 7 7 • Function taking list and both pointers as parameters • Taking (integer) data as input • Checking if list is full … • … and returning False • Insert the input data to the empty list node’s data • Following pointers to find last node in Linked List … • …and updating last node’s pointer to empty list/location where new node is added • Updating empty list to it’s first elements pointer • Returning true when added successfully Example code: Visual Basic Function addNode(ByRef linkedList() As node, ByVal currentPointer As Integer, ByRef emptyList As Integer) Console.WriteLine("Enter the data to add") Dim dataToAdd As Integer = Console.ReadLine() Dim previousPointer As Integer = 0 Dim newNode As node If emptyList < 0 Or emptyList > 9 Then Return False Else newNode.data = dataToAdd newNode.nextNode = -1 1(d)(i) linkedList(emptyList) = newNode previousPointer = 0 While (currentPointer <> -1) previousPointer = currentPointer currentPointer = linkedList(currentPointer).nextNode End While Dim valueToWrite As Integer = emptyList linkedList(previousPointer).nextNode = valueToWrite emptyList = linkedList(emptyList).nextNode Return True End If End Function Python def addNode(linkedList, currentPointer, emptyList): dataToAdd = input("Enter the data to add") if emptyList <0 or emptyList > 9: return False else: newNode = node(int(dataToAdd), -1) linkedList[emptyList] = (newNode) previousPointer = 0 while(currentPointer != -1): previousPointer = currentPointer currentPointer = linkedList[currentPointer].nextNode linkedList[previousPointer].nextNode = emptyList emptyList = linkedList[emptyList].nextNode return True 1(d)(i) Java public static Boolean addNode(node[] linkedList, Integer currentPointer, Integer emptyList){ Integer dataToAdd; Integer previousPointer; node newNode; Scanner in = new Scanner(System.in); System.out.println("Enter the data to add"); dataToAdd = in.nextInt(); if(emptyList < 0 || emptyList > 9){ return false; }else{ newNode = new node(dataToAdd, -1); linkedList[emptyList] = newNode; previousPointer = 0; while(currentPointer != -1){ previousPointer = currentPointer; currentPointer = linkedList[currentPointer].nextNode; } linkedList[previousPointer].nextNode = emptyList; emptyList = linkedList[emptyList].nextNode; return true; } } 1(d)(ii) 1 mark per bullet point 3 • Call addNode() with list, start and empty pointers and store/check return value … • …output appropriate message if True returned and if False returned • Calling outputNodes() with list and start pointer before and after addNode() Example code: Visual Basic Sub Main() Dim linkedList(10) As node linkedList(0).data = 1 linkedList(0).nextNode = 1 linkedList(1).data = 5 linkedList(1).nextNode = 4 linkedList(2).data = 6 linkedList(2).nextNode = 7 linkedList(3).data = 7 linkedList(3).nextNode = -1 linkedList(4).data = 2 linkedList(4).nextNode = 2 linkedList(5).data = -1 linkedList(5).nextNode = 6 linkedList(6).data = -1 linkedList(6).nextNode = 7 linkedList(7).data = 56 linkedList(7).nextNode = 3 linkedList(8).data = -1 linkedList(8).nextNode = 9 linkedList(9).data = -1 linkedList(9).nextNode = -1 Dim startPointer As Integer = 0 Dim emptyList As Integer = 5 outputNodes(linkedList, startPointer) Dim returnValue As Boolean returnValue = addNode(linkedList, startPointer, emptyList) 1(d)(ii) If returnValue = True Then Console.WriteLine("Item successfully added") Else Console.WriteLine("Item not added, list full") End If outputNodes(linkedList, startPointer) Console.ReadLine() End Sub Python linkedList = [node(1,1),node(5,4),node(6,7),node(7,-1),node(2,2),node(-1,6), node(-1,7),node(56,3),node(-1,9),node(-1,-1)] startPointer = 0 emptyList = 5 outputNodes(linkedList, startPointer) returnValue = addNode(linkedList, startPointer, emptyList) if returnValue == True: print("Item successfully added") else: print("Item not added, list full") outputNodes(linkedList, startPointer) Java public static void main(String[] args){ node[] linkedList = new node[10]; linkedList[0] = new node(1,1); linkedList[1] = new node(5, 4); linkedList[2] = new node(6, 7); linkedList[3] = new node(7,-1); linkedList[4] = new node(2,2); linkedList[5] = new node(-1,6); linkedList[6] = new node(-1,7); linkedList[7] = new node(56, 3); linkedList[8] = new node(-1,9); 1(d)(ii) linkedList[9] = new node(-1,-1); Integer startPointer = 0; Integer emptyList = 5; outputNodes(linkedList, startPointer); Boolean returnValue; returnValue = addNode(linkedList, startPointer, emptyList); if (returnValue == true){ System.out.println("Item successfully added"); }else{ System.out.println("Item not added, list full"); } outputNodes(linkedList, startPointer); } 1(d)(iii) 1 mark for screenshot showing : 1 • Linked list output • 5 input • Message saying Successfully added or equivalent • Linked list output with 5 at the end. Example: 1 5 2 6 56 7 5 (being input) 1 5 2 6 56 7 5