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

CS335 Test 1 Solutions: Reversing Lists, Summing Integers, and Circular Linked Lists, Exams of Data Structures and Algorithms

The solutions to test 1 of cs335, which covers topics such as reversing lists using adt operations, summing integers recursively, and printing items in a circular linked list.

Typology: Exams

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

59 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS335, TEST 1: MONDAY, OCTOBER 9, 2006
Solutions
(1) (10 pts) Recall the pseudocode versions of the ADT List oper-
ations
createList()
destroyList()
boolean isEmpty()
integer getLength()
insert(in index:integer, in newItem:ListItemType,
out success:boolean)
remove(in index:integer, out success:boolean)
retrieve(in index:integer, out dataItem:ListItemType,
out success:boolean)
Write a pseudocode function
List reverse(in List:oldList)
which takes in a List object oldList as a parameter and returns
a new list with the same elements as oldList but in the op-
posite order. You should make use of the ADT List operations
above (and not any details about the particular implementation
of the List ADT). So, for example, if oldList contains the list
of integers 1,2,7,12,5, your reverse function should return
the list 5,12,7,2,1.
Solution:
List reverse(in List:oldList){
newList.createList()
len = oldList.getLength()
for (i=1; i <= len; ++i){
oldList.retrieve(len - i + 1, item, dummy)
newList.insert(i, item, dummy)
}
return newList
}
1
pf3
pf4
pf5

Partial preview of the text

Download CS335 Test 1 Solutions: Reversing Lists, Summing Integers, and Circular Linked Lists and more Exams Data Structures and Algorithms in PDF only on Docsity!

Solutions

(1) (10 pts) Recall the pseudocode versions of the ADT List oper- ations

  • createList()
  • destroyList()
  • boolean isEmpty()
  • integer getLength()
  • insert(in index:integer, in newItem:ListItemType, out success:boolean)
  • remove(in index:integer, out success:boolean)
  • retrieve(in index:integer, out dataItem:ListItemType, out success:boolean)

Write a pseudocode function List reverse(in List:oldList) which takes in a List object oldList as a parameter and returns a new list with the same elements as oldList but in the op- posite order. You should make use of the ADT List operations above (and not any details about the particular implementation of the List ADT). So, for example, if oldList contains the list of integers 1,2,7,12,5, your reverse function should return the list 5,12,7,2,1. Solution: List reverse(in List:oldList){ newList.createList() len = oldList.getLength() for (i=1; i <= len; ++i){ oldList.retrieve(len - i + 1, item, dummy) newList.insert(i, item, dummy) } return newList }

1

(2) (10 pts) Consider the List of strings

car, house, boat, truck Let the following pseudocode execute (refer to the ADT List operations on the previous page and RECALL OUR CONVEN- TION THAT WE START COUNTING FROM INDEX 1, NOT 0, IN A LIST): remove(2,success1) insert(3,"deck",success2) retrieve(6,asset,success3) (Here, success1, success2 and success3 are three boolean variables, "deck" is a string constant, and asset is a string variable.) (a) Write down the list after all these three lines have executed. Solution: car, boat, deck, truck (b) Also, what are the values (true or false) of each of the three boolean variables success1, success2, success3 after the pseudocode has executed? Solution: success1 is true, success2 is true, and success is false. (c) What is the value of the string variable asset after the pseudocode has executed? (In fact, does it have a well- defined value at all?) Explain. Solution: asset doesn’t have a well-defined value, since the position 6 is past the end of the list. (3) (10 pts) Write a recursive C++ function

int sumInts(int n) which returns the sum of the integers 1 + 2 + · · · + n. Solution: int sumInts(int n){ if (n<=0) return 0; // avoid infinite recursion else return (n + sumInts(n-1)); }

(5) (10 pts) Consider the class CircularList for a circular linked list: the data members of the class are class CircularList{/* public members here ... / private: struct Node{ ListItemType item; Node next; }; // end struct Node Node* list; /points to the node BEFORE the first node/ }; Write the C++ code for a member function void CircularList::printList() which prints out all the items in a given circular list. (Note there is no size data member to record how many items there are, and no NULL pointer at the end of the list. How can you tell whether you have successfully printed out all the items in the list just once?) Example: If your circular linked list is represented by

your printList function should print out 3 4 7 2 (or 2 3 4 7; either is acceptable). Solution: void CircularList::printList(){ if (list!=NULL){ Node* curr = list; do{ cout << curr->item << " "; curr = curr->next; }while (curr!=list); cout << endl; } }

(6) (10 pts) The following program performs a binary search of an array. What is the output (to the screen)? #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,5) << endl; } Solution: binarySearch: first = 0, last = 8. binarySearch: first = 0, last = 3. binarySearch: first = 2, last = 3. 2