## CSCI 3300 Fall 2013 Exercises for Quiz 4

Some of these questions use the following type Cell, which is the same as the Cell type used in class.

```    struct Cell
{
Cell* next;
int item;

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

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

```    struct Gadget
{
int puff;
int stuff;

{
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;

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;

3. Using type Gadget, 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?

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

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. 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));

6. 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;
4. t = L->item->next;
5. t = L->next->item;

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

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.

```Cell* addToFront(int x, Cell* L)
```

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 Cell* L)
```

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 Cell* L)
```

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 Cell* L)
```

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.

```  Cell* tail(Cell* L)
{
Cell* p = L;
delete p;