Past-paper question · 2017 May-June · Paper 41 · Q4(c)(vi)

Question 4(c)(vi) 2017 · May-June · Paper 41 · 6 marks

Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Original past-paper question for 4(C)(VI)
Reveal official mark-scheme answer Official mark-scheme answer for 4(C)(VI)

Official answer for Question 4

Part (c)

(vi)

✓
mark as follows:
  • Store free list pointer as NewNodePointer
  • Store new data item in free node
  • Adjust free pointer
  • F list is currently empty
  • Make the node the first node
  • Set pointer of this node to Null Pointer
  • Find insertion point
  • If previous pointer is Null pointer
  • Link this node to front of list
  • Link new node between Previous node and next node
✓
Python Example
✓
def AddToList(self, NewData):
✓
NewNodePointer = self.__FreeListPointer
✓
self.__NodeArray[NewNodePointer].SetData(NewData)
✓
self.__FreeListPointer =
✓
self.__NodeArray[self.__FreeListPointer].GetPointer()
✓
if self.__HeadPointer == -1:
✓
self.__HeadPointer = NewNodePointer
✓
self.__NodeArray[NewNodePointer ].SetPointer(-1)
✓
else:
✓
PreviousPointer, NextPointer = self.FindInsertionPoint(NewData)
✓
if PreviousPointer == -1 :
✓
self.__NodeArray[NewNodePointer ].SetPointer
✓
(self.__HeadPointer)
✓
self.__HeadPointer = NewNodePointer
✓
else:
✓
self.__NodeArray[NewNodePointer ].SetPointer(NextPointer)
✓
self.__NodeArray[PreviousPointer].SetPointer(NewNodePointer)
✓
9608/41
✓
Cambridge International AS/A Level – Mark Scheme
✓
May/June 2017
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
Pascal Example
✓
procedure LinkedList.AddToList(NewData : string);
✓
var NewNodePointer , PreviousPointer,
✓
NextPointer : integer;
✓
begin
✓
// make a copy of free list pointer
✓
NewNodePointer := FreeListPointer;
✓
// store new data item in free node
✓
NodeArray[NewNodePointer].SetData(NewData);
✓
// adjust free pointer
✓
FreeListPointer :=
✓
NodeArray[FreeListPointer].GetPointer;
✓
// if list is currently empty
✓
if HeadPointer = -1
✓
then
✓
// make the node the first node
✓
begin
✓
HeadPointer := NewNodePointer;
✓
// set pointer to Null pointer
✓
NodeArray[NewNodePointer].SetPointer(-1);
✓
end
✓
else
✓
// find insertion point
✓
begin
✓
FindInsertionPoint(NewData, PreviousPointer,
✓
NextPointer);
✓
// if previous pointer is Null pointer
✓
if PreviousPointer = -1
✓
then
✓
// link node to front of list
✓
begin
✓
NodeArray[NewNodePointer]
✓
.SetPointer(HeadPointer);
✓
HeadPointer := NewNodePointer ;
✓
end
✓
else
✓
// link new node between
✓
Previous node and next node
✓
begin
✓
NodeArray[NewNodePointer ]
✓
.SetPointer(NextPointer);
✓
NodeArray[PreviousPointer]
✓
.SetPointer(NewNodePointer);
✓
end;
✓
end;
✓
end;
✓
9608/41
✓
Cambridge International AS/A Level – Mark Scheme
✓
May/June 2017
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
VB.NET Example
✓
Public Sub AddToList(ByVal NewData As String)
✓
Dim NewNodePointer, PreviousPointer, NextPointer As Integer
✓
' make copy of free list pointer
✓
NewNodePointer= FreeListPointer
✓
' store new data item in free node
✓
NodeArray(NewNodePointer).SetData(NewData)
✓
' adjust free pointer
✓
FreeListPointer = NodeArray(FreeListPointer).GetPointer
✓
' if list iscurrently empty
✓
If HeadPointer = -1 Then
✓
' make the node the first node
✓
HeadPointer = NewNodePointer
✓
' set pointer to Null pointer
✓
NodeArray(NewNodePointer).SetPointer(-1)
✓
Else
✓
' find insertion point
✓
FindInsertionPoint(NewData, PreviousPointer,
✓
NextPointer)
✓
' if previous pointer is Null pointer
✓
If PreviousPointer = -1 Then
✓
' link to front of list
✓
NodeArray(NewNodePointer).SetPointer(HeadPointer)
✓
HeadPointer = NewNodePointer
✓
Else
✓
' link new node between Previous node and next node
✓
NodeArray(NewNodePointer).SetPointer(NextPointer)
✓
NodeArray(PreviousPointer).SetPointer(NewNodePointer)
✓
End If
✓
End If
✓
End Sub
✓
9608/41
✓
Cambridge International AS/A Level – Mark Scheme
✓
May/June 2017
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
Pseudocode for reference:
✓
PROCEDURE AddToList(NewData)
✓
// remember value of free list pointer
✓
NewNodePointer ← FreeListPointer
✓
// add new data item to free node pointed to by free list
✓
NodeArray[NewNodePointer].Data ← NewData
✓
// adjust free pointer to point to next free node
✓
FreeListPointer ← NodeArray[FreeList].Pointer
✓
// is list currently empty?
✓
IF HeadPointer = NullPointer
✓
THEN
✓
// make the node the first node
✓
HeadPointer ← NewnodePointer
✓
// set pointer of new node to Null pointer
✓
NodeArray[NewNodePointer].Pointer ← NullPointer
✓
ELSE
✓
// find insertion point
✓
CALL FindInsertionPoint(NewData, PreviousPPointer, NextPointer)
✓
// if previous pointer is Null pointer
✓
IF PreviousPointer = NullPointer
✓
THEN
✓
// link new node to front of list
✓
NodeArray[NewNodePointer].Pointer ← HeadPointer
✓
HeadPointer ← NewNodePointer
✓
ELSE
✓
// link new node between previous node and next node
✓
NodeArray[NewNodePointer].Pointer ← NextPOinter
✓
NodeArray[PreviousPointer].Pointer ← NewNodePointer
✓
END IF
✓
ENDIF
✓
END PROCEDURE

Mark scheme 9608_s17_ms_41.pdf →

Show text version of question

Question 4

Part (c)

(vi)

Question 4

An ordered linked list Abstract Data Type (ADT) has these associated operations:

  • create list
  • add item to list
  • output list to console

The ADT is to be implemented using object-oriented programming as a linked list of nodes.

Each node consists of data and a pointer. The identifier table for the LinkedList class is:

Identifier Data type Description HeadPointer INTEGER Pointer to the first node in the ordered list. FreeListPointer INTEGER Pointer to the first node in the free list. NodeArray ARRAY[0 : 7] OF Node 1D array stores the nodes that make the ordered linked list. The unused nodes are linked together into a free list. Constructor() Constructor instantiates an object of LinkedList class, initialises HeadPointer to be a null pointer and links all nodes to form the free list. FindInsertionPoint() Procedure that takes the new data item as the parameter NewData and returns two parameters:

  • PreviousPointer, whose value is:
  • either pointer to node before the insertion point or the null pointer if the new node is to be inserted at the beginning of the list.

  • NextPointer, whose value is a

pointer to node after the insertion point. AddToList(NewString) Procedure that takes as a parameter a unique string and links it into the correct position in the ordered list. OutputListToConsole() Procedure to output all the data from the list pointed to by HeadPointer.

The following diagram shows an example of a linked list object. This example list consists of three nodes, linked in alphabetical order of the data strings. The unused nodes are linked to form a free list.

HeadPointer Berlin London Paris Ø node node node

FreeListPointer Ø node node node node

The symbol O represents a null pointer.

(vi)

The structured English for the AddToList(NewString) method is as follows:

Make a copy of the value of free list pointer, name it NewNodePointer

Store new data item in free node pointed to by NewNodePointer

Adjust free list pointer to point to next free node

IF linked list is currently empty

THEN

Make this node the first node

Set pointer of this node to null pointer

ELSE

Find insertion point using the FindInsertionPoint method

// FindInsertionPoint provides

// pointer to previous node and pointer to next node

IF previous pointer is null pointer

THEN

Link this node to front of list

ELSE

Link this node between previous node and next node

The FindInsertionPoint method receives the new data item as the parameter NewString. It returns two parameters:

  • PreviousPointer, whose value is:
  • either the pointer to the node before the insertion point or the null pointer, if the new node is to be inserted at the beginning of the list.

  • NextPointer, whose value is the pointer to the node after the insertion point.

Question 15

Write program code for the AddToList method.

Programming language used ............................................................................................

Program code

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[6]

Question 16

reasonable effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

Topic relevance

Why this question appears here

98% assigned-topic match · Further programming › Programming paradigms

Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.

Permanent source page: Educate A Change · From 9608_s17_qp_41.pdf

Keep practising this topic