{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COS110-tutorial10-Memo

# COS110-tutorial10-Memo - COS 110 Tutorial 10 Department of...

This preview shows pages 1–3. Sign up to view the full content.

COS 110 Tutorial 10 Department of Computer Science 19 October MEMO Please use own discretion when marking, the answers can diver from the memo. The students repose should do what the question asks; if it works they should get marks. The ticks represent 1 mark. In the coding answers the mark is given in [] for each line to mark. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1. Use the linked list in Chapter 17 as the base (pg 1081 - 1085). This uses templates; this means u must also use template functions. (a) (6) Create a function that will add a node at a speciﬁed index. Position 0 is the start of the list, a position of 1 is the second index in the list and a position equal to or greater than the list size is the last index. The function prototype is : void addAtIndex(T,int), the T is the value and int is the index. Solution: 1 tenplate < class T> 2 void LinkedList<T>:: addAtIndex (T newValue , int index ) 3 { 4 ListNode * newNode ; 5 ListNode * norePtr ; 6 ListNode * previousNode = NULL; 7 8 int counter = 0; [ 0 . 5 ] 9 10 newNode = new ListNode ; 11 newNode - >value = num; 12 13 i f ( ! head ) 14 { 15 head = newNode ; 16 newNode - >next = NULL; 17 } 18 else 19 { 20 nodePtr = head ; 21 22 previousNode = NULL; 23 24 while 25 { 26 previousNode = nodePtr ; 27 nodePtr = nodePtr - >next ; 28 counter++; [ 0 . 5 ] 29 } 30 31 i f for t e s t i n g i f i t i s the new head ] 32 { 33 head = newNode ; [ 0 . 5 ] 34 newNode - >next = nodePtr ; [ 0 . 5 ] 35 } 36 else i f ( nodePtr - >next == null ) [ 0 . 5 , for t e s t i n g i f i t i s the new t a i l ] 37 { 38
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

COS110-tutorial10-Memo - COS 110 Tutorial 10 Department of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online