=========================preview======================
(COMP152)[2010](f)midterm~2385^_10035.pdf
Back to COMP152 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY

Department of Computer Science & Engineering
COMP 152: Object-Oriented Programming and Data Structures
Fall 2010
Midterm Examination

Instructor: Gary Chan

Date: Saturday, 23 October 2010
Time: 2:30pm C 4:00pm
Venue: LTC

.
This is a closed-book examination. However, you are allowed to bring with you a piece of A4-sized paper with notes written or typed on both sides for use in the examination.

.
Your answers will be graded according to correctness, ef.ciency, precision, and clarity.

.
During the examination, you must put aside your calculators, mobile phones, PDAs and all other electronic devices. In particular, all mobile phones should be turned off completely.

.
This booklet has 21 single-sided pages. Please check that all pages are properly printed before you start working.

.
You may use the reverse side of the pages for your rough work or continuation of work.


Student name: English nickname (if any): Student ID: Email: Assigned ID:
I have not violated the Academic Honor Code in this examination (your signature):

Question
Your score
Maximum score




1

20
2
30
3
25
4
25

TOTAL
100


1. Basic C++: Pointer, Recursion and File I/O (20 points total) In this question, you are going to implement two functions to build and delete a ring structure,
respectively.
You are given the following RingNodede.nition which holds a char:

class RingNode{

public:
char value; // value stored in the node
RingNode* next; // pointer to the next RingNode

};

You are also given the following function to print the ring, given a pointer to the beginning of the ring (given by head) and the size of the ring (given by size):
// print the ring
// head: pointer to the beginning of the ring
// size: number of nodes in the ring
void print_the_ring(const class RingNode* head, int size){

for (int i = 0; i < size; i++){
cout << head->value << endl;
head = head->next;

}
}

(a) (12 points) Implement a recursive read function to build a ring given a .le stream with the prototype:
int read_recursively (ifstream& fin, RingNode*& current);

This function takes the finof ifstreamas input and reads in the .rst line of the .le to build a ring, i.e., it reads character by character of the line until either end of .le or \nis encountered, whichever is earlier. You can assume that finis opened correctly.
The function takes in the RingNode pointer current, which has been initialized to NULL. The function returns the number of nodes in the ring, with current pointing to the node of the .rst character in the line. If there is no character before end-of-.le or \n, then currentshould be NULL. For example, given the following codes:
int main(){
ifstream fin("input.txt");
RingNode* start_point = NULL;
int total = read_recursively(fin, start_point);
print_the_ring(start_point,total);

//....

If the content o