Question 3: Both of the following programs reads every line from standard input.
ID: 3738058 • Letter: Q
Question
Question 3: Both of the following programs reads every line from standard input. One of them prints the lines out in their original order, the other prints them out in reverse order. Which is which? (You should be able to tell just by seeing which ADT each is using.) import eduprinceton.cs.algs4.* public class Program public static void main(Stringl] args) public static void main( String[] args) Stack String stk new Stack String 0: Queue Stringqnew QueueString 0: import edu.princeton.cs.algs4. public class Program2 while (Stdin.hasNexLine)) f String line StdinreadLine qenqueueline: while (Stdn.hasNextLine) String line Stdin.readLine) stk.push line) for String line:stk) StdOut.printin(line); for (String line: Stdoutprintln ine Note the use of the enhanced for loop to iterate through the items in the stack or queue. Remember that they iterate through the items on the stack or queue in pop () and dequeue () order, respectively.Explanation / Answer
Q3.
Program1 - Reverse order (as it is stack, LIFO)
Program2 - Original order (as it is queue, FIFO)
Part1
Q1:
a.pop() - This removes and returns the top most element of the stack
b.dequeue() - This removes and returns the first element of the queue
c.push() - This pushes an element in the stack
d.size() - This gives the number of elements in stack and queue
e.isEmpty() - This returns true if queue/stack is empty, otherwise false
f.enqueue() - This adds an element to the queue at the back.
Q2:
The correct one:
t.next = x.next;
x.next = t
The other one is incorrect because it breaks the linked list at
t as t.next is pointing to t itself.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.