CSC235 Data Structures - List Homework

  1. Given the following picture of a linked list, where P and Q are of type ptrType: Write the code that will manipulate the linked list as follows. Each question should use the original list shown above.

    1. Make P point to the node containing the number 10.
    2. Make Q point to the node containing the number 8.
    3. Make P point to the node containing the number 6.
    4. Make the node containing the number 8 point to the node containing the number 6.
    5. Make the node containing the number 10 point to the node containing the number 8.
    6. Make the node containing the number 6 point to the node containing the number 10.
    7. Add the Item field in the node P points to and the Item field in the node Q points to and store the result in the node that currently contains 10.
    8. Remove the node containing 12 from the list.


  2. Given the following linked list, where P, Q and S are of type ptrType: Show how each of the following statements changes the linked list. Draw a similar diagram and show the resulting list. Each question should be answered starting with the original list shown above.

    1. S->Item = P->Item;
    2. P->Item = Head->Item + Q->Item;
    3. S = Q->Next;
    4. P->Next = Head;
    5. Head->Next = Q->Next;
    6. Q->Next = S->Next;
    7. Q = Q->Next;
    8. P->Next = P;


  3. Write a member function called traverse in the unordered list class that prints out the contents of each node in the list. If the list is empty, print the message "empty list".

  4. Write a member function called last in the unordered list class that returns the item in the last node of the list.

  5. Write a member function called makeRev that has one parm, an unordered list, which sets the parm to contain the same values as the invoking object, but in the opposite order.

  6. Use the unordered list methods to write the statements to delete every other node in the list called mylist, starting with the first node.

  7. Suppose the value 10 might be in the list mylist more than once. Use the unordered list methods to write the statements to delete all occurrences of the value 10 from the list.

  8. Use the unordered list methods to write the statements to create a list called revlist that contains the same values as the list mylist, but in the opposite order.

  9. Suppose p, q, and r are declared as type node*. Show what is printed for each of the following, or explain why an error occurs:
    a.   p = new node;                       b.   p = new node;
         p->Item = 3;                             p->Next = new node;
         q = new node;                            q = p->Next;
         q->Item = 7;                             q->Next = NULL;
         q->Next = p;                             p->Item = 5;
         p = new node;                            q->Item = 10;
         p->Item = q->Item;                       r = new node;
         cout << p->Item << q->Item <<            q->Next = r;
              q->Next->Item << endl;              q->Item = r->Item;
                                                  r->Item = p->Item + 1;
                                                  cout << r->Item << 
                                                       q->Item << p->Item << endl;
    
    

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page