



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!
(1) (10 pts) Recall the pseudocode versions of the ADT List oper- ations
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