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

here is a java homework question. please help! You are given a stack a adda queu

ID: 3567814 • Letter: H

Question

here is a java homework question. please help!

You are given a stack a adda queue q of Integers. Write client level statements to create a new stack s2 that merges s and q. The new stack contains the reverse contents of the queus on top of the reversed contents from the stack.

for example, if s(shown from top to bottom)is:

1 4 7 8

and q (shown from first to last) is:

1,2,3,4,6,9

then s2 (shown from top to bottom)will be:

9,6,4,3,2,1,8,7,4,1

Use only the standard stack methods push,pop,peek,and isEmpty and the standard queue methods enqueue,dequeue,getFront,and isEmpty.

you do not need to write a complete method. The original stack and queue can be modiffied or destroyed.

thanks for everyone.

Explanation / Answer

//Merging a queue and stack elements into a new stack using the method merge

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;


public class StackQueueMergeProgram
{
   public static void main(String[] args)
   {
      
       //create an instace of stack and push element
       //into the stack
       Stack<Integer>integerStack=new Stack<Integer>();
      
       integerStack.push(8);
       integerStack.push(7);
       integerStack.push(4);
       integerStack.push(1);
      
      
       System.out.println("Stack elements");
       //display elements in the stack
       System.out.println(integerStack.toString());
       //Since queue is an interface ,use LinkedList to
       //implement the queue because it not possible to
       //create an object for queue directly since queue
       //is an interface in java.
      
       //create an instance of Queue and add elements to the queue.
       Queue<Integer>integerQueue=new LinkedList<Integer>();
       integerQueue.add(1);
       integerQueue.add(2);
       integerQueue.add(3);
       integerQueue.add(4);
       integerQueue.add(6);
       integerQueue.add(9);
      
      
       System.out.println("Queue elements");
       //display elements in the queue.
       System.out.println(integerQueue.toString());
      
      
       //call mergeQueueStack with arguments Queue and stack as its ojects
       Stack<Integer>newStack=mergeQueueStack(integerQueue,integerStack);
      
      
       System.out.println("New merged Stack elements");
       //display the final merge stack elements
       System.out.println(newStack.toString());
      
      
      
      
   }

  
   //The method mergeQueueStack accepts two arguments ,a queue object and
   //stack object and returns a final stack object with elements merged
   //from both the queue and stack as required without destroying original
   //queue and stack.
  
   //Steps inside the method.
   //First move queue elemenst to a new temporary stack.
   //Mover stack elements to a new temporary stack.
   //create a final stack to store both stack elements
   //Pop elements from the first stack to new final stack
   //Pop elements from the second stack to new final stack
   private static Stack<Integer> mergeQueueStack(Queue<Integer> integerQueue,
           Stack<Integer> integerStack) {

      
       //create a new stack to store merged stack and queue
       Stack<Integer>finalStack=new Stack<Integer>();
      
       //create a temporray stack to move elements from queue to stack
       Stack<Integer>tempStakForQueue=new Stack<Integer>();
      
       while (!integerQueue.isEmpty())
       {
           tempStakForQueue.push(integerQueue.remove());
       }
      
       //create temp stack to move elements from stack to a new stack
       Stack<Integer>tempStack=new Stack<Integer>();
      
      
       while (!integerStack.isEmpty()) {
           tempStack.push(integerStack.pop());
       }
      
       //Move elements from tempQueue to final stack.
      
       while(!tempStakForQueue.isEmpty())
           finalStack.push(tempStakForQueue.pop());
      
       //Move elements from tempStack to final stack
       while(!tempStack.isEmpty())
           finalStack.push(tempStack.pop());
      
      
      
      
      
       return finalStack;
   }
}

------------------------------------------------------------------------------------
Sample output:

Stack elements
[8, 7, 4, 1]
Queue elements
[1, 2, 3, 4, 6, 9]
New merged Stack elements
[9, 6, 4, 3, 2, 1, 8, 7, 4, 1]

Hope this would be helpful to you