COS110Tutorial 11-memo

COS110Tutorial 11-memo - University of Pretoria COS110...

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

View Full Document Right Arrow Icon
1 University of Pretoria COS110 Tutorial 11 MEMO-MEMO-MEMO Date: 26 October 2011 Time: 50 minutes Marks: 35 Student Surname and Initials:____________________________________________ Student number:__________________ Employee number:___________________ Question 1 Doubly linked lists [6 marks] (Scope: Lecture 31) Study the class definition below for a doubly linked list class: class NumberList //--------------- { public: NumberList(); ~NumberList(); void appendNode(int val); void printReverse(); private: struct ListNode { int value; struct ListNode *next; struct ListNode *prev; }; ListNode *head; }; Complete the code below to print the list contents in reverse. (6 marks) void NumberList::printReverse() {
Background image of page 1

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

View Full DocumentRight Arrow Icon
//start at the beginning of the list and locate //the last node in the list ________________________________________________ ________________________________________________ ________________________________________________ //now move towards beginning printing the value in each node ________________________________________________ ________________________________________________ ________________________________________________ ________________________________________________ ________________________________________________ } Solution: void NumberList::printReverse() { //start at the beginning of the list and locate //the last node in the list nodePtr = head; while (nodePtr->next != NULL) nodePtr = nodePtr->next; (3 marks) //now move towards beginning printing the value in each node while (nodePtr->prev != head) { cout << nodePtr->value << endl; nodePtr = nodePtr->prev; } (3 marks) } Question 2 Stack ADT and static stacks [16 marks] (Scope: Lectures 31, textbook sections 18.1) A class for the stack ADT to hold integer values has been defined as follows: class Stack { public: Stack(int size); ~Stack( ); void push(int newVal); int pop( ); int peek( );
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course IT 101 taught by Professor Unsure during the Spring '11 term at University of South Africa.

Page1 / 6

COS110Tutorial 11-memo - University of Pretoria COS110...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online