Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed Notes. No Questions and NO PDAs (calculators, handheld devices, cell phones, etc.) are allowed. Q1) Consider the following ordered linked list. Each node is based on the struct named nodetype,which is defined as follows: struct nodetype int info; nodetype* link; First Last 3 3 5 7 9 10 Count C Q1.1) What is the output of the following code? (12 Points; 3 points each) cout << First->info << endl; cout << (First->link->info == c->info)<<endl; cout << c->link->link->info << endl; c = c->link->link; cout << c->info << endl; 3 0 9 9 Page 1 of 5
Q1.2) Write a single C++ statement to do the following (9 Points) A. Make the pointer c points to the Second Node with info equals to 3? (3 Points) c = First->link; B. Set the info of first node to 2 instead of 3? (3 Points) First->info = 2; C. Set the info of last node to 15 instead of 10? (3 Points) Last->info = 15; Q2) Consider the following figure which represents a doubly linked list. Each node is based on the struct named nodetype, which is defined as follows: struct nodetype int info; nodetype* next; nodetype* back; First p Last Count 5 2 4 8 10 Q2.1) What is the output of the following code? (12 Points; 3 points each) cout << p->next->back->info; cout << First->next->next->info; p = p->back; cout << p->back->next->next->info; cout << Last->back->back->info; Page 2 of 5
Q2.2) Write the required code to insert a new node with number 7 between and 8. There is no need to search for the required location just add the node pointed to by q. Please ignore the changes that you have made in the previous question. (12 Points) q 7 q->next = p->next; q->back = p; p->next->back = q; p->next = q; Q3) When you answer the questions 3.1, 3.2, 3.3, and 3.4, please consider carefully the definition of the following program: #include <iostream> using namespace std; struct node int info; node * link; ; class linkedlisttype private: node *head; public: linkedlisttype() head = NULL; int getnodescount(void); // Returns the number of nodes currently available in the linked list int replacealloccurr(int oldval, int newval) // Replaces all occurrences of the oldval with newval within the list int findsecondoccurr(int item); // Finds the position (Node Number) of the second occurrence of an item // within the list. // More member functions for usual list operations ; Page 3 of 5
Q3.1) Write the implementation of the member function named: int getnodescount(void); It returns the number of nodes currently available in the linked list. (11 Points) int linkedlisttype::getnodescount() s while( p!= NULL ) // 2 Points // 2 Points Q3.2) Write the implementation of the member function named: int replacealloccurr(int oldval, int newval); This function finds all occurrences of an existing value (oldval) and replaces them with a new value (newval) and returns the number of replaced item. (12 Points) int linkedlisttype::replacealloccurr(int oldval, int newval) s while( p!= NULL ) if (p->info == oldval) p->info = newval; // 2 Point Page 4 of 5
Q3.3) Write the implementation of the member function named: int findsecondoccurr(int item); It returns the node number of the second occurrence of an item within the list. Otherwise, if the item is not present in the list for at least two times, this function should return a zero. (22 Points) int linkedlisttype::findsecondoccurr(int item) bool found; bool first; found = false; first = false; while( p!= NULL &&!found) if ( p->info == item ) if (first == false) first = true; else found = true; if (found) else return 0; // 2 Point Q3.4) What is the Time-Complexity (Big O notation) for the functions you provided in: (10 Points) Q3.1: O(n) Q3.2: O(n) Q3.3: O(n) // 4 Points Page 5 of 5