I want to solve this problem WITHOUT putting it into an Java IDE. (If it came up in an exam I would not be able to solve it.)
So what I know so far.
I know that stack is referred to as LIFO and queue is referred to as FIFO. But I do not understand what this code would pop out (Or if I'm correct).
public static void main (String[] args) {
Queue<String> q = new Queue<String> ();
q.enqueue ("one");
q.enqueue ("two");
q.enqueue ("four");
q.enqueue ("six");
String s = "";
int i = 0;
while (!q.isEmpty()) {
s = s + q.dequeue().substring(i);
i++;
}
StdOut.print (s);
}
Since it is a queue it would be FIFO and the substring would cause the output to be: onewour? Since s.substring() for six would be (3) so no value would be present.
And lastly I found two problems in a Princeton CS class but do not know how the answer came to be
Suppose that a client performs an intermixed sequence of (queue) enqueue and dequeue operations. The enqueue operations put the integers 0 through 9 in order onto the queue; the dequeue operations print out the return value. Which of the following sequence(s) could not occur?
(a) 0 1 2 3 4 5 6 7 8 9
(b) 4 6 8 7 5 3 2 9 0 1
(c) 2 5 6 7 4 8 9 3 1 0
(d) 4 3 2 1 0 5 6 7 8 9
Answer: (b), (c), and (d).
Suppose that an intermixed sequence of (stack) push and pop operations are performed. The pushes push the integers 0 through 9 in order; the pops print out the return value. Which of the following sequence(s) could not occur?
(a) 4 3 2 1 0 9 8 7 6 5
(b) 4 6 8 7 5 3 2 9 0 1
(c) 2 5 6 7 4 8 9 3 1 0
(d) 4 3 2 1 0 5 6 7 8 9
(e) 1 2 3 4 5 6 9 8 7 0
(f) 0 4 6 5 3 8 1 7 2 9
(g) 1 4 7 9 8 6 5 3 0 2
(h) 2 1 4 3 6 5 8 7 9 0
Answer: (b), (f), and (g).