CSCI 3300
Spring 2009
Exercises for Quiz 6

These questions use the following type Cell.

    struct Cell
    {
      Cell* next;
      int item;

      Cell(int i, Cell* n)
      {
        item = i;
        next = n;
      }
    };

  1. Write a statement that will create variable L, of type Cell*, and make it point to new linked list holding only one value, 30.

    Answer

  2. Which of the following will create variable L and make it point to a new linked list holding 1 and 2, in that order?

    1. Cell* L = new Cell*(1, new Cell*(2, NULL));
    2. Cell* L = new Cell*(2, new Cell*(1, NULL));
    3. Cell* L = new Cell(1, new Cell(2, NULL));
    4. Cell* L = new Cell(2, new Cell(1, NULL));
    5. Cell* L = new Cell(new Cell(1, 2, NULL));

    Answer

  3. Suppose that L is a variable of type Cell* that has already been created and t is a variable of type int. Linked list L has at least two numbers in it. Which of the following sets variable t to the second integer in list L?

    1. t = L.item.next;
    2. t = L.next.item;
    3. t = L[2];
    4. t = L->item->next;
    5. t = L->next->item;

    Answer

  4. Which of the following sequences of statements correctly removes the first cell of a linked list L, making L point to the rest of the list, and deletes the removed cell?

    1. Cell* t = L; L = L->next; delete L;
    2. Cell* t = L; L = L->next; delete t;
    3. Cell* t = L->next; L->next = L; delete t;
    4. Cell* t = L; delete t; L = L->next;
    5. Cell* t = L; L->next = L; delete t;

    Answer

  5. Write a C++ definition of function addToFront(x,L) which returns the list that you get by adding number x to the front of list L.

    Cell* addToFront(int x, Cell* L)
    

    Answer

  6. Write a C++ definition of function isSingleton(L) that returns true if L is a singleton list, and false otherwise. (A singleton list has exactly one number in it.)

    bool isSingleton(const Cell* L)
    

    Answer

  7. Write a C++ definition of function firstPositive(L) that returns the first positive number in linked list L. If L does not contain a positive number, then firstPositive(L) should return 0. Remember that L is a linked list, not an array.

    int firstPositive(const Cell* L)
    

    Answer

  8. Write a C++ definition of function isSorted(L), which returns true if L is in strictly ascending order and returns false if L is not in ascending order. The empty list is considered to be in ascending order.

      bool isSorted(const Cell* L)
    

    Answer

  9. Explain why the following is not a correct definition of function tail, which returns the result of removing the first member of linked list L.

      Cell* tail(Cell* L)
      {
        Cell* p = L;
        Cell* answer = p->next;
        delete p;
        return answer;
      }
    

    Answer

  10. Explain why the following is not a correct definition of function addToFront, from a previous exercise.

      Cell* addToFront(int x, Cell* L)
      {
        Cell c;
        c.item = x;
        c.next = L;
        return &c;
      }
    

    Answer