SAMPLE Final Questions

CS 162: Introduction to Computer Science II

 

1) Fundamental Linked List Questions (25 points)

Given the following node structure:

 

            struct node {

                        char * name;

                        char phone[21];

                        node * next;

            };

 

            node * head;

           

a)      Show the C++ code allocate memory for 1 node, dynamically

 

head = new node;

 

b)      Now, show how to allocate memory for the name, sized just right

//char some_name[100];

//cin.get(some_name, 100);

 

head->name = new char [strlen(some_name) + 1];

 

c)      Show how to store your name and phone number in this node

 

strcpy(head->name, some_name);

cin.get(head->phone,21); cin.ignore(100,’\n’);

 

d)     Show how to delete this one node

 

delete [] head->name;

delete head;

 

e)      Now, how can you tell if the list is empty?

 

if (head == NULL)          //if (!head)

 

 

 

 

 

 

 

2. (25 points) Assume the following class builds a linear linked list:

           

2a. Write at least 4 prototypes and all of the data members for managing a LLL of names/email addresses. You may use the node struct from the previous page.

 

      *** All data must be obtained by the calling routine – NOT from the user! ***

 

class list {                          //maintains a linked list address book

      public:                        

//Create at least 4 member function prototypes here

 

 

 

 

 

      private:

 

 

};

 

2b. Write the code for the constructor:

 

 

 

 

 

 

 

2c. Write the code for the destructor:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(25 points)

 

3. C++ Coding Questions.

 

Assume that you have a linear linked list of just integers

 

  1. Write the code to insert a node at the END of an existing linear linked list.

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Write the code to display every other integer in the linear linked list

 

 

 

 

 

 

 

 

 

 

 

 

  1. Write the code to delete all nodes in a LLL

 

 

 

 

 

 

 

 

 

 

3. (25 points) Short Answer and pointer arithmetic

 

a. When should we pass pointers by reference?

 

 

 

 

 

 

 

Write the code to insert a new name into the list, at the end (given only the head pointer -- not a tail pointer)

 

            void ordered::insert(char nme[], char a_code[], char ph[]) {

 

3. (25 points) Pointer Questions

 

a) Assume that you need to pass a pointer to an integer by reference to a function (named search_int), what would the function call and the function prototype look like:

 

            variable definitions: int * ptr;

           

 

            function call:         search_int(ptr);

 

 

            function prototype:…………..void search_int(int * & ptr);

 

b) Show how to allocate an array of 20 integers dynamically at run-time

 

ptr = new int [ 20];

 

c) Show how to later on, deallocate that same memory.

 

delete [] ptr;              

 

 

c) Explain why we can’t pass an “array” by reference:

 

An array is a constant pointer to the first element. You can’t pass a constant by reference. It would be like trying to change the location of the actual array (sort of like moving a house)