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

Code Fragment - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Code Fragment, Programming Language, Spaces Provided, Partial Credit, Function Extend, Consider this Function, Last Elements

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt 🇮🇳

4.3

(8)

159 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:___________________________________
CS 111: Data Structures
EXAM #2
Wednesday October 24, 2012
Checking whether build environment is sane … build environment is grinning and holding a spatula. Guess not.
http://xkcd.com/371/
Do not open this exam until you are instructed to do so.
This is a closed book, closed notes, closed electronics, closed neighbor, pencil and paper exam. All
programming answers are to be written in the C++ programming language.
This exam is designed to be completed in 1 hour. If you have any questions during the exam you may
find your professor in his office (room 2016). The exam is due at 9:00 PM sharp. Turn it in directly to
your professor.
Write answers in the spaces provided. Indicate your final answers clearly. Cross out any work you do
not want graded. Show your work and explain your reasoning where appropriate; this will help to
award partial credit.
For Grading:
Q1 ________ Q3 _________ Q5 _________
Q2 ________ Q4 __________ tot _________
1
pf3
pf4
pf5

Partial preview of the text

Download Code Fragment - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Name:___________________________________

CS 111: Data Structures

EXAM

Wednesday October 24 , 201 2

Checking whether build environment is sane … build environment is grinning and holding a spatula. Guess not. http://xkcd.com/371/

Do not open this exam until you are instructed to do so.

This is a closed book, closed notes, closed electronics, closed neighbor, pencil and paper exam. All programming answers are to be written in the C++ programming language. This exam is designed to be completed in 1 hour. If you have any questions during the exam you may find your professor in his office (room 2016). The exam is due at 9:00 PM sharp. Turn it in directly to your professor. Write answers in the spaces provided. Indicate your final answers clearly. Cross out any work you do not want graded. Show your work and explain your reasoning where appropriate; this will help to award partial credit. For Grading: Q1 ________ Q3 _________ Q5 _________ Q2 ________ Q4 __________ tot _________

There is a doubly-linked list, q points to a node in the list, and p points to a node to be inserted before q. Write a code fragment to do that. For example, Before: After: Take good care. There are no sentinels in this list representation.

p

q

p

Consider this function. Rewrite it without using a loop construct. You may define and use a tail-recursive helper function. float sqrt(float x) { float v = x/2; while (abs(v*v-x) > 0.0000001) { v = (v + x / v) / 2.0; } return v; }

Suppose you have two linked lists list1 and list2 Write the function interleave that rebuilds them into one list (with these same nodes) starting with the first node in list1, then the first node in list2, then the second node in list1, then the second node in list2, etc., until all nodes are represented. If one list is longer than another, its last elements will remain consecutive in the result. Here's an example: Before: After the statement p = interleave(list1, list2): You are welcome to simply fill in the blanks in this outline, or, if you prefer, write the function some other way. (But you can't do both. Cross out what you don't want graded.) Node * interleave(Node * list1, Node * list2) { // Base cases: if (__________________________) return ______________; if (__________________________) return ______________; // Recursive case (just 3 statements) : Node * z = interleave(__________________, _______________); _____________ = z; return _____________; } list A C E list B D F G A B C D E F p G

class List { public: typedef int ElementType; List(); // Create an empty doubly-linked list with front and rear sentinels, // current points to rear, and currentIndex = 0; ~List(); // Delete all nodes List(const List & orig); // Make a deep copy of a list void add(const ElementType & item,size_t index); // Insert a new element at position index // PRE: List contains <v0, v1, ..., vindex, ...> // 0 <= index <= getSize() // POST: List contains <v0, v1, ..., item, vindex, ... > // current points to new node containing item. void removeAt(size_t index); // Remove an element from the list. // Pre: 0 <= index < getSize() // List contains <v0, v1, ..., vindex, v(index+1), ... > // Post: List contains <v0, v1, ..., v(index+1), ... > // current points to v(index+1) void remove(const ElementType & item); // Let i be the smallest integer where get(i)==item. // Post: same as for removeAt(i) // If no such i exists, no change to the list. size_t find(const ElementType & item) const; // return smallest integer where get(i)==item, // or getSize() if no such integer ElementType get(size_t index) const; // return the element at position i. size_t getSize() const; // return the number of elements of the list // runs in constant time std::ostream & output(std::ostream & ostr) const; // output the list to ostr using format // <v0, v1, v2, ..., vn-1> private: // (omitted, not relevant) };