Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Member Function - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Member Function, Cout Statements, Private Members, Empty List, Newitem, Doublenode, Member Function

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt ๐Ÿ‡ฎ๐Ÿ‡ณ

4.3

(8)

159 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures, Sample Test 1, with Answers
1. A doubly linked list is based on the following data:
class DoubleList{
// public function declarations here ...
private:
struct DoubleNode{
ListItemType item; // data in the node
DoubleNode* next; // points to next node
DoubleNode* precede; // points to previous node
};
DoubleNode* head; // points to first node
};
(a) Write a member function
void DoubleList::removeFirstNode()
which deletes the first node from a doubly linked list.
(b) Assume the DoubleNode* variable curr points to a node in our dou-
bly linked list. Write a few lines of code to remove this node *curr
from the list. (Be careful: what should happen if curr is at the
beginning or end of the list?)
(c) Write a member function
DoubleList::DoubleNode* DoubleList::find(int index)
which returns a pointer to the node at position index in the doubly
linked list. Your function should return NULL if the index is out of
range (note our class DoubleList does not have a size data member:
how can you determine if index is out of range in this case?)
pf3
pf4
pf5

Partial preview of the text

Download Member Function - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures, Sample Test 1, with Answers

  1. A doubly linked list is based on the following data:

class DoubleList{ // public function declarations here ... private: struct DoubleNode{ ListItemType item; // data in the node DoubleNode* next; // points to next node DoubleNode* precede; // points to previous node }; DoubleNode* head; // points to first node };

(a) Write a member function void DoubleList::removeFirstNode() which deletes the first node from a doubly linked list. (b) Assume the DoubleNode* variable curr points to a node in our dou- bly linked list. Write a few lines of code to remove this node curr from the list. (Be careful: what should happen if curr is at the beginning or end of the list?) (c) Write a member function DoubleList::DoubleNode DoubleList::find(int index) which returns a pointer to the node at position index in the doubly linked list. Your function should return NULL if the index is out of range (note our class DoubleList does not have a size data member: how can you determine if index is out of range in this case?)

  1. What is the output (to the screen) of the following program? Be sure you pay attention to all the cout statements.

#include int binarySearch(const int anArray[], int first, int last, int value){ cout << "binarySearch: first = " << first << ", last = " << last << ".\n"; if (first>last) return -1; else{ int mid = (first + last)/2; if (value == anArray[mid]) return mid; else if (value < anArray[mid]) return binarySearch(anArray, first, mid-1, value); else return binarySearch(anArray, mid+1, last, value); } // end else } // end binarySearch int main(){ int a[] = {1,4,5,10,12,18,25,31,107}; cout << binarySearch(a,0,8,12) << endl; cout << binarySearch(a,0,8,15) << endl; }

  1. What is the output of the following program?

#include #include int main(){ for (int i=1; i<=3; ++i){ try{ if (i%2 == 0) throw exception(); cout << "i is now " << i << endl; } // end try catch (exception e){ cout << "error\n"; } // end catch } // end for } // end main

  1. Consider the ADT List operations

(a) Create an empty list. (b) Destroy a list. (c) Determine whether a list is empty. (d) Determine the number of items in a list. (e) Insert an item at a given position in the list. (f) Delete the item at a given position in the list. (g) Look at (retrieve) the item at a given position in the list.

Using only the ADT List operations (a)-(g) above, describe how to replace an item at a given position on a list with a new item. (So, for example, given the list of integers 0,3,8,9,4,10, if the fourth item is to be replaced with a 7, the new list will be 0,3,8,7,4,10.) You may write your answer in (precise) words, in pseudocode, or in representative C++ code.

Answers

  1. (a) void DoubleList::removeFirstNode(){ if (head != NULL){ DoubleNode* temp = head; head = head->next; delete temp; temp = NULL; if (head != NULL) head->precede = NULL; } } (b) DoubleNode* prev = curr->precede; DoubleNode* following = curr->next; if (prev != NULL) prev->next = following; if (following != NULL) following->precede = prev; delete curr; (c) DoubleList::DoubleNode* DoubleList::find(int index){ if (index<1) return NULL; DoubleNode* curr = head; for (int i=1; i<index; ++i){ if (curr == NULL) return NULL; curr = curr->next; } // end for return curr; }
  2. binarySearch: first = 0, last = 8. 4 binarySearch: first = 0, last = 8. binarySearch: first = 5, last = 8. binarySearch: first = 5, last = 5. binarySearch: first = 5, last = 4.
  3. i is now 1 error i is now 3
  4. (a)

head

  • โˆ’โ†’ 5 โ€ข โˆ’โ†’ 3 โ€ข โˆ’โ†’ 4 โ€ข โˆ’โ†’ 7 โ€ข โˆ’โ†’ 12 โ€ข โˆ’โ†’ 10 NULL

(b) 3