Docsity

Final Exam Practice Problems - Data Structure | CSCI 1200, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Spring 2006
Final Exam Practice Problems
General Information
•The final exam will be held Thursday, May 11th, 2006, 6:30-9:30pm, DCC 308. A
makeup exam will be offered if required by the RPI rules regarding final exam conflicts -OR-
if a written excuse from the Dean of Students office is provided. Contact Professor Cutler
ASAP if you require a makeup exam.
•Coverage: Lectures 1-25, Labs 1-14, HW 1-8.
•Closed-book and closed-notes except for 2 sheets of 8.5x11 inch paper (front & back) that may
be handwritten or printed. Computers, cell-phones, palm pilots, calculators, PDAs, etc. are
not permitted and must be turned off.
•All students must bring their Rensselaer photo ID card.
•Below are relevant sample questions covering material after Test 3. Do not consider this list
complete, and remember the final exam will be cumulative.
•The best thing you can do to prepare for the final is practice. Try these problems with pencil &
paper first. Then practice programming (with a computer) these exercises and other exercises
from lecture, lab, homework and the textbook.
Practice Problems
1. Write a function to create a new singly-linked list that is a copy of a sublist of an existing list.
The prototype is:
Node<T>* Sublist(Node<T>* head, int low, int high)
The Node class is:
template <class T>
class Node {
public:
T value;
Node* next;
};
The new list will contain high-low+1 nodes, which are copies of the values in the nodes
occupying positions low up through and including high of the list pointed to by head. The
function should return the pointer to the first node in the new list. For example, in the
following drawing the original list is shown on top and the new list created by the function
when low==2 and high==4 is shown below.
1
pf3

Partial preview of the text

Download Final Exam Practice Problems - Data Structure | CSCI 1200 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Final Exam Practice Problems

General Information

  • The final exam will be held Thursday, May 11th, 2006, 6:30-9:30pm, DCC 308. A makeup exam will be offered if required by the RPI rules regarding final exam conflicts -OR- if a written excuse from the Dean of Students office is provided. Contact Professor Cutler ASAP if you require a makeup exam.
  • Coverage: Lectures 1-25, Labs 1-14, HW 1-8.
  • Closed-book and closed-notes except for 2 sheets of 8.5x11 inch paper (front & back) that may be handwritten or printed. Computers, cell-phones, palm pilots, calculators, PDAs, etc. are not permitted and must be turned off.
  • All students must bring their Rensselaer photo ID card.
  • Below are relevant sample questions covering material after Test 3. Do not consider this list complete, and remember the final exam will be cumulative.
  • The best thing you can do to prepare for the final is practice. Try these problems with pencil & paper first. Then practice programming (with a computer) these exercises and other exercises from lecture, lab, homework and the textbook.

Practice Problems

  1. Write a function to create a new singly-linked list that is a copy of a sublist of an existing list. The prototype is:

Node* Sublist(Node* head, int low, int high)

The Node class is:

template class Node { public: T value; Node* next; };

The new list will contain high-low+1 nodes, which are copies of the values in the nodes occupying positions low up through and including high of the list pointed to by head. The function should return the pointer to the first node in the new list. For example, in the following drawing the original list is shown on top and the new list created by the function when low==2 and high==4 is shown below.

New list

head 3.1 2.4 8.7 9.

nhead 2.4 8.7 9.

14.2 0.

Original list

A pointer to the first node of this new list should be returned. (In the drawing this would be the value of nhead.) You may assume the original list contains at least low nodes. If it contains fewer than high nodes, then stop copying at the end of the original list.

  1. Suppose that a monster is holding you captive on a computational desert island, and has a large file containing double precision numbers that he needs to have sorted. If you write correct code to sort his numbers he will release you and when you return home will be allowed to move on to DSA. If you don’t write correct code, he will eventually release you, but only under the condition that you retake CS 1. The stakes indeed are high, but you are quietly confident — you know about the standard library sort function. (Remember, you are supposed to have forgotten all about bubble sort.) The monster startles you by reminding you that this is a computational desert island and because of this the only data structure you have to work with is a queue. After panicking a bit (or a lot), you calm down and think about the problem. You realize that if you maintain the values in the queue in increasing order, and insert each value into the queue one at a time, then you can solve the rest of the problem easily. Therefore, you must write a function that takes a new double, stored in x, and stores it in the queue. Before the function is called, the values in the queue are in increasing order. After the function ends, the values in the queue must also be in increasing order, but the new value must also be among them. Here is the function prototype:

void insert_in_order(double x, queue& q)

You may only use the public queue interface (member functions) as specified in lab. You may use a second queue as local variable scratch space or you may try to do it in a single queue (which is a bit harder). Give an “O” estimate of the number of operations required by this function.

  1. Write a cs2list member function called reverse that reverses the order of the nodes in the list. The head pointer should point to what was the tail node and the tail pointer should point to what was the head node. All directions of pointers should be reversed. The function prototype is:

template void cs2list::reverse();

The function must NOT create ANY new nodes.