CSCI 3300
Fall 2009
Exercises for Quiz 6

These questions use the following type ListCell.

    struct ListCell
    {
      ListCell* next;
      int item;

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

  1. Suppose that the following structure type definition is given.

        struct Gadget
        {
          int puff;
          int stuff;
    
          Gadget(int p, int s)
          {
            puff  = p;
            stuff = s;
          }
        };
    
    Which of the following will create a Gadget called g, stored in the run-time stack, whose puff variable contains 4 and whose stuff variable contains 8?
    1. Gadget g(puff = 4, stuff = 8);
    2. Gadget g; puff.g = 4; stuff.g = 8;
    3. Gadget g; puff = 4; stuff = 8;
    4. Gadget g(4, 8);
    5. Gadget g = Gadget(puff = 4, stuff = 8);

    Answer

  2. If g is the Gadget created in the preceding question, which of the following will print the value of the stuff variable in Gadget g on the standard output?

    1. cout << g[stuff];
    2. cout << g.stuff;
    3. cout << stuff.g;
    4. cout << Gadget g.stuff;
    5. cout << stuff(Gadget);

    Answer

  3. Using type Gadget from the previous question, which of the following will create a new Gadget in the heap, with its puff variable holding 39, and its stuff variable holding 4, and make variable w point to the new Gadget?

    1. Gadget* w = new Gadget(39,4);
    2. Gadget* w = new Gadget(4,39);
    3. new Gadget w(39,4);
    4. new Gadget w(4,39);
    5. new Gadget* w(39, 4);

    Answer

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

    Answer

  5. 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. ListCell* L = new ListCell*(1, new ListCell*(2, NULL));
    2. ListCell* L = new ListCell*(2, new ListCell*(1, NULL));
    3. ListCell* L = new ListCell(1, new ListCell(2, NULL));
    4. ListCell* L = new ListCell(2, new ListCell(1, NULL));
    5. ListCell* L = new ListCell(new ListCell(1, 2, NULL));

    Answer

  6. Suppose that L is a variable of type ListCell* 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

  7. 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. ListCell* t = L; L = L->next; delete L;
    2. ListCell* t = L; L = L->next; delete t;
    3. ListCell* t = L->next; L->next = L; delete t;
    4. ListCell* t = L; delete t; L = L->next;
    5. ListCell* t = L; L->next = L; delete t;

    Answer

  8. 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.

    ListCell* addToFront(int x, ListCell* L)
    

    Answer

  9. 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 ListCell* L)
    

    Answer

  10. 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 ListCell* L)
    

    Answer

  11. 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 ListCell* L)
    

    Answer

  12. 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.

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

    Answer