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

In java Move the contents of a stack to a queue. Maintain order so that if you w

ID: 3814165 • Letter: I

Question

In java

Move the contents of a stack to a queue. Maintain order so that if you were to pop and print all of the items from the stack and dequeue and print all die items from the queue it would print the same thing. Move all the contents of a queue to a stack. Maintain order so that if you were to pop and print of the items from the stack and dequeue and print all the items from the queue it would print the same thing. Move the contents of a stack to another stack. Maintain order so that if you were to pop and print the items of both stacks, they would print the same tiling.

Explanation / Answer

Since you have not mentioned anything i assume i have to use the build in classes of JAVA.

import java.util.*;


public class stackQueueConvertion
{

public static Queue<Integer> stackToQueue(Stack<Integer> st){

Queue<Integer> q = new LinkedList<Integer>();
  
while(!st.empty()){
q.add((Integer)st.pop());
}
  
return q;
}

public static Stack<Integer> queueToStack(Queue<Integer> q){

Stack<Integer> st = new Stack<Integer>();
LinkedList<Integer> list = new LinkedList<Integer>();
Integer val = null;
int i = 0;

while( (val = (Integer)q.poll()) != null){
list.add(val);
}

for(i=list.size()-1; i>=0 ;i--){
st.push((Integer)list.get(i));
}

return st;
}


public static Stack<Integer> stackToStack(Stack<Integer> st){

LinkedList<Integer> list = new LinkedList<Integer>();
Stack<Integer> st2 = new Stack<Integer>();
int i =0 ;

while(!st.empty()){
list.add((Integer)st.pop());
}

for(i= list.size() - 1; i >= 0 ;i--){
st2.push((Integer)list.get(i));
}

return st2;
}

public static void main(String []args)
{
Stack<Integer> st1 = new Stack<Integer>();
int i = 1;
Integer val = null;


for(i=1; i<=10;i++){
st1.push(new Integer(i*i));
}

Queue<Integer> q = stackToQueue(st1);

st1 = queueToStack(q);
Stack<Integer> st2 = stackToStack(st1);

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote