Test 1: CPS 100 Owen Astrachan October 5, 1994 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 TOTAL: value 8 pts. 6 pts. 14 pts. 10 pts. 14 pts. 52 pts. grade 1
PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context of computer science, programming, and C/C++. 1. template (a) A program used as a pattern on which similar programs are designed and implemented. (b) A mechanism in C++ for parameterizing a class by atype, e.g., allowing a general stack class to be able to represent a stack ofint and a stack of double. (c) A mechanism that permits operators to be overloaded in a user-dened class, e.g., so that += can mean something for a class like BigInt. 2. destructor (a) A member function that is automatically invoked when a variable of a user-dened type is initially dened. (b) A member function that is automatically invoked when a variable of a user-dened type goes out of scope. (c) A bug in a program that slowly stops the operating system from working. 3. O-notation (a) A notation used to express relationships between dierent objects in an object-oriented program. (b) A notation used to express resource requirements of an algorithm that provides an asymptotic upper bound on the resource. (c) A notation used by the professor in this course to describe his own programs. 4. Queue (a) A last-in, rst-out data structure. (b) A rst-in, rst-out data structure. (c) A letter in the alphabet. PROBLEM 2 : (Postx: 6 points) What is the value of the postx expression: 8 2 * 4 5 + - 11 * Write a postx expression equivalent to 7 + (12 + 3) * 4. Note that this expression has the value 67. How many comparisons are needed to nd an element in an array of four million elements using binary search? [answers within 10 will be considered correct]. 2
PROBLEM 3 : (Stacks: 12 points) Part of the header le for the stack class we discussed in class is shown below (this is for a stack ofint.) const int maxsize = 400; typedef int ElementType; class Stack public: Stack (); // constructor Stack (const Stack &); // copy constructor ~Stack(); // destructor int IsEmpty () const; // returns 1 if empty, else 0 ElementType Top () const; // returns top element int Push(ElementType); // push, return 1 if success int Pop(); // pop, return 1 if success Stack & operator = (const Stack &); private: int tos; // top of stack ElementType * elements; // array implementation ; Part A 8points You are to write the body of the NON-member function StackSize below. StackSize returns the number of elements stored in the parameter s. Note that s is passed as a const-reference parameter which means that the member function Pop CANNOT be used with s (since this would alter s). You can, however, dene variables/objects of type Stack. int StackSize(const Stack & s) // postcondition: returns # of elements stored in stack s 3
Part B 3points Write the body of operator < below. In writing the function you may call the function StackSize from part A. Assume that StackSize works as specied, regardless of what you wrote for part A. Less-than is dened for stacks a and b so that a<bif the number of elements in stack a is smaller than the number of elements in stack b. int operator < (const Stack & a, const Stack & b) // postcondition: returns 1 if stack a is "smaller" than stack b. Part C 3points In the denition of the class Stack given above an array is used to store the elements of the stack. The array is accessible via the private data eld elements. The body of the constructor Stack is shown below. Stack::Stack() // postcondition: empty stack initialized elements = new ElementType [maxsize]; tos = 0; Explain how this constructor could be changed so that a statement like Stack s(100); could be used to dene a stack of 100 elements, but the denition Stack st; would still be legal and dene a stack of 400 elements. 4
Bonus question (2 points): Who invented the language C++? 5
PROBLEM 4 : (Double-link 10 points) In a doubly-linked list, each node maintains pointers to the previous node and to the next node. For example, the diagram below shows a doubly-linked list of 4 nodes. 5 7 11 16 Doubly-linked lists are implemented using the struct below. struct Node int info; Node * next; Node * previous; Node(int datum, Node * before = 0, Node * after = 0); ; Node::Node(int datum, Node * before, Node * after) // postcondition: construct list node from given params info = datum; previous = before; next = after; Part A Assume that doubly-linked lists are maintained in sorted order and do NOT have header nodes. Write the function Insert below that inserts a new node containing val into list so that list remains in sorted order. void Insert(Node * & list, int val) // precondition: list is sorted, list = a1, a2,..., an // postcondition: list is sorted, list contains val and all nodes a1,...,an // 6
7
PROBLEM 5 : (Classied 14 points) A class that implements lists of strings uses the denition below. The denitions below support adding elements only to the front of a List. The underlying representation of a List uses a linked list with a header node. typedef String Key; struct Node Key info; Node * next; Node(const Key & key, Node * follow = 0) info = key; next = follow; ; class List public: List(); List(const List & list); ~List(); int Size() const; void AddFront(const Key & key); int Includes(const Key & key); private: Node * first; ; // constructor, makes empty list // copy constructor // destructor // returns # of elements in list // add key to front of list // returns 1 if key in list // points to first node in list The use of a header node means that a list ("apple","pear", "orange","banana") is represented by an object diagrammed below. A constructor initializes an empty list consisting of just a header node whose next eld has value 0. first apple pear orange banana List::List() // postcondition: list with 0 elements created first = new Node("",0); // make header node 8
Part A: 6points Assume that the operator += will be overloaded so that it adds a new node to the end of a List. That is the eect of animals += "zebra" is to add a new item to the end of the List animal. The prototype for this operator is given below, you are to write the code implementing the operator. List & List::operator += (const Key & key) // precondition: *this represents a1, a2,..., an // postcondition: *this represents a1, a2,..., an, key 9
Part B: 8points Assume that the function Remove whose header and specication are given below is provided. You do NOT need to write this function, you can call the function in code you write. void Remove(Node * & list, const String & word) // postcondition: all nodes of list containing word have been removed // other nodes remain in original order A new member function Uniquify is to be implemented that removes all duplicate keys from a list. For example, if a list fruit is represented by ("apple","pear", "apple", "orange", "apple", "pear", "banana") then after the call fruit.uniquify(), fruit should represent the list ("apple","pear", "orange", "banana") Write member function Uniquify and describe, using O-notation, the complexity of the code you write to remove all duplicates from a list that initially contains n elements. 10