Help with the following: 1. Draw a depiction of the Stack after each operation(
ID: 3569318 • Letter: H
Question
Help with the following:
1. Draw a depiction of the Stack after each operation( be sure to label the top and the bottom) and write the output of the following code:
StackS = new Stack();
S.push("A");
S.push("B");
System.out.println(S.pop());
S.push("C");
System.out.println(S.peek());
System.out.println(S.pop());
2. Draw the depiction of the queue as each of the command is executed
queueQ = new queue();
Q.enqueue("A");
Q.enqueue("B");
String x = Q.dequeue();
Q.enqueue("C");
String y = Q.dequeue();
Q.enqueue("D");
3. Compare the relative efficiency of the push and pop methods of a stack based on whether it is internally implemented as a linked list or as an array. Are there any special configurations that must be considered?
Explanation / Answer
StackS = new Stack(); //Empty Stack
S.push("A"); //A
S.push("B"); //B A
System.out.println(S.pop()); //B
S.push("C"); //C A
System.out.println(S.peek()); //C A
System.out.println(S.pop()); //C
2)
queueQ = new queue(); //Empty Queue
Q.enqueue("A"); //A
Q.enqueue("B"); //B
String x = Q.dequeue(); //x = A
Q.enqueue("C"); //B C
String y = Q.dequeue(); y = B
Q.enqueue("D"); //C D
3)Pushing and poping are both O(1) cost operations
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.