Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Top of Form Question 1 and 2 may be difficult to answer- these can be optionally

ID: 3564143 • Letter: T

Question

Top of Form

Question 1 and 2 may be difficult to answer- these can be optionally answered- although it would be nice to get answer

-------------------------------------------------------------------------------------------------------------------------------------------------

This may be helpful for question 1 & 2

-------------------------------------------------------

public static void main(String[] args)

{ // Create a stack and push/pop strings as directed on StdIn.

Stack s = new Stack();

while (!StdIn.isEmpty())

{

String item = StdIn.readString();

if (!item.equals("-"))

s.push(item);

else if (!s.isEmpty()) StdOut.print(s.pop() + " ");

}

StdOut.println("(" + s.size() + " left on stack)");

}

Test client for Stack

-------------------------------------------------------------------------------------------------------------------

1.3 ? Bags, Queues, and Stacks 147

-------------------------------------------------------------------------------------------------------------------

public static void main(String[] args)

{ // Create a queue and enqueue/dequeue strings.

Queue q = new Queue();

while (!StdIn.isEmpty())

{

String item = StdIn.readString();

if (!item.equals("-"))

q.enqueue(item);

else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");

}

StdOut.println("(" + q.size() + " left on queue)");

}

Test client for Queue

-----------------------------------------------------------------------------------------

Give the output printed by the book's Stack client for the input

    it was - the best - of times - - - it was - the - -

Question 1 options:

was best times of the was the it

was best times it the was the of

it was the best of times it was

Give the output printed by the book's Queue client for the input

    it was - the best - of times - - - it was - the - -

Question 2 options:

was best times of the was the it

was best times it the was the of

it was the best of times it was

Question 3:

What does the following code fragment do to the queue q?

    Stack stack = new Stack();
    while (!q.isEmpty())
       stack.push(q.dequeue());
    while (!stack.isEmpty())
       q.enqueue(stack.pop());

Question 3 options:

reverses the order of the elements in q

randomizes the order of the elements in q

does nothing to the order of the elements in q

it is not possible to say what the effect is

Question 4:

Suppose that an intermixed sequence of push and pop operations are performed on a LIFO stack. The pushes push the numbers 0 through 9 in order; the pops print out the return value. Which of the following output sequence(s) could occur? Select all that are possible

question 4 options:

Question 5:

Suppose that an intermixed sequence of enqueue and dequeue operations are performed on a FIFO queue. The enqueues add the numbers 0 through 9 in order; the dequeues print out the return value. Which of the following output sequence(s) could occur? Select all that are possible.

Question 5 options:

was best times of the was the it

was best times it the was the of

it was the best of times it was

Explanation / Answer

question 1) (a) was best times of the was the it

question 2) (c) it was the best of times it was

question 3) (a) reverses the order of the elements in q

question 4) (a,b) 1 2 5 4 3 6 0 9 8 7 and 6 5 4 3 2 1 0 7 8 9

question 5) (d) 0 1 2 3 4 5 6 7 8 9