CS 2005, B Term 1999
Data Structures and Programming Techniques
Practice Problems for Test 2

(also see the self-test exercises
in chapters 5-10 of the textbook)

Your Name: _____________________________________

Your Lab Section: ______________     Your Login Name:_________________

Instructions.   Read each problem carefully. Write your solutions in ink in the spaces provided. Write neatly and explain your answers as clearly as possible.

In the following problems, all linked lists contain nodes of the following Node type, defined in the file "link1.h":

struct Node {
   typedef int Item;
   Item data;
   Node *link; 
};
A linked list is referred to by giving a "head pointer" that points to the first Node of the list. The last Node of the list contains the NULL pointer in its link member. The empty list has NULL as its head pointer.

  1. For each of the following, write a C++ program segment that accomplishes the stated objective. Declare all variables that are used in your program segment, except for the given head pointers.

  2. (50 points) Implement the linked list function list_head_insert specified below.
    //   void list_head_insert(Node*& head_ptr, const Node::Item& entry) 
    //     Precondition: head_ptr is the head pointer of a linked list.
    //     Postcondition: A new node containing the given entry has been added at
    //     the head of the linked list; head_ptr now points to the head of the new,
    //     longer linked list.
    
    
       
       
       
       
       
    
    
    

      Refer to the following documentation for a basic Stack template class. Any desired enhancements to the Stack class must be explicitly documented and implemented.

    // FILE: stack2.h  (by Main and Savitch)
    // TEMPLATE CLASS PROVIDED: Stack<Item> (a stack of items)
    //   The template parameter, Item, is the data type of the items in the Stack.
    //   It may be any of the C++ built-in types (int, char, etc.), or a class
    //   with a default constructor, copy constructor, and assignment operator.
    //
    // CONSTRUCTOR for the Stack<Item> template class:
    //   Stack( )
    //     Postcondition: The stack has been initialized as an empty Stack.
    //
    // MODIFICATION MEMBER FUNCTIONS for the Stack<Item> class:
    //   void push(const Item& entry)
    
    //     Postcondition: A new copy of entry has been pushed onto the Stack.
    //
    //   Item pop( )
    //     Precondition: size( ) > 0.
    //     Postcondition: The top item of the stack has been popped and returned.
    //
    // CONSTANT MEMBER FUNCTIONS for the Stack<Item> class:
    //   size_t size( ) const
    //     Postcondition: Return value is the total number of items in the Stack.
    //
    //   bool is_empty( ) const
    //     Postcondition: Return value is true if the Stack is empty.
    //
    //   Item peek( ) const
    //     Precondition: size( ) > 0.
    //     Postcondition: Return value is top item on Stack (Stack is unchanged).
    
    
    Use objects of the following BoardPosition type to store board positions:
    struct BoardPosition {
       int row;  // row number corresponding to given board position
       int col;  // column number corresponding to given board position
    };
    

  3. For each of the following, write a C++ program segment that accomplishes the stated objective. Use the Stack template class and the BoardPosition type as as described in the specification given above. Declare all variables used in your program segment.

  4. (50 points) Implement the function print_stack specified below.
    //   void print_stack(Stack<int>& anystack)
    //   Postcondition: all of the items contained in anystack have been printed 
    //     to the console output stream, with exactly one item per line; 
    //     control has returned normally to the context that called print_stack.
    
    
       
          
       
    
    
    
      Use the following definition of the BinaryTreeNode type. As usual, an empty tree (or subtree) is indicated by a NULL root pointer. Any binary tree functions used must be explicitly documented and implemented unless otherwise stated.
        template <class Item> 
        struct BinaryTreeNode
        {
            Item data;                   // by the way, g++ will accept this
            BinaryTreeNode<Item>* left;  // declaration even if the template
            BinaryTreeNode<Item>* right; // parameter <Item> is missing here
        };
    

  5. For each of the following, write a C++ program segment that accomplishes the stated objective. Declare all variables used in your program segment. Variables for which value assignments are not explicitly requested are assumed to be initialized elsewhere in the program. All binary tree nodes used in your solution should store integers in their data members.

  6. (50 points) Implement the C++ function tree_size specified below. Hint: use recursion. You must implement any binary tree functions invoked within your function.
    template <class Item>
    size_t tree_size(const BinaryTreeNode<Item> *root_ptr)
    //  Precondition: root_ptr points to the root node of a binary 
    //  tree (or root_ptr may be NULL to indicate the empty tree).
    //  Postcondition: the number of nodes in the tree has been returned.