These questions use the following type ListCell.
struct ListCell
{
ListCell* next;
int item;
ListCell(int i, ListCell* n)
{
item = i;
next = n;
}
};
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?
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?
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?
Write a statement that will create variable L, of type ListCell*, and make it point to new linked list holding only one value, 30.
Which of the following will create variable L and make it point to a new linked list holding 1 and 2, in that order?
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?
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?
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)
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)
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)
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)
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;
}