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

Using Java, in this problem two stacks A and B are to be manipulated using the f

ID: 3815494 • Letter: U

Question

Using Java, in this problem two stacks A and B are to be manipulated using the following operations (n denotes the size of A and m the size of B):

- PushA (x): Push element x on stack A.

- PushB (x): Push element x on stack B.

- MultiPopA (k): Pop min {k, n} elements from A.

- MultiPopB (k): Pop min {k, m} elements from B.

- Transfer (k): Repeatedly pop an element from A and push it on B, until either k elements have been moved or A is empty.

It is assumed that A and B are to be implemented using doubly-linked lists such that PushA and PushB , as well as a single pop from A or B, can be performed in O (1) time worst-case.

Implement the the above operations as their own methods. You are allowed to use the code provided under Support Documents as a starting point.

Explanation / Answer

import java.util.Scanner;
import java.util.Stack;

public class StackProblem {

   public static int n = 0;
   public static int m = 0;
   public static Stack<Integer> B = new Stack<Integer>();
   public static Stack<Integer> A = new Stack<Integer>();
   public static Scanner scanner = new Scanner(System.in);
   public static void main(String[] args) {
       int choice;
       System.out.println("Enter Size of A");
       n = scanner.nextInt();
       System.out.println("Enter Size of B");
       m = scanner.nextInt();
       do{
           System.out.println("Operations : ");
           System.out.println("1. Push to A");
           System.out.println("2. Push to B");
           System.out.println("3. Multipop A");
           System.out.println("4. Multipop B");
           System.out.println("5. Transfer A to B");
           System.out.println("6. Exit");
           System.out.print("Select operation to be done : ");
           choice = scanner.nextInt();
           int x,k;
           switch (choice)
           {
               case 1:
                       System.out.println("Enter value to be pushed to A : ");
                       x=scanner.nextInt();
                       PushA(A,x);
                       break;
               case 2:      
                       System.out.println("Enter value to be pushed to B : ");
                       x=scanner.nextInt();
                       PushB(B,x);
                       break;
               case 3:
                       System.out.println("Enter number of values to be popped from A : ");
                       k=scanner.nextInt();
                       MultiPopA(A,n,k);
                       break;
               case 4:
                       System.out.println("Enter number of values to be popped from B : ");
                       k=scanner.nextInt();
                       MultiPopB(B,m,k);
                       break;
               case 5:
                       System.out.println("Enter number of values to be transfered from A to B : ");
                       k=scanner.nextInt();
                       Transfer(A,B,n,m,k);
                       break;
               default:
                       break;
                          
           }
       }while(choice!=6);
   }
  
   static void PushA(Stack<Integer> A, int a) {
   A.push(new Integer(a));
   System.out.println("push(" + a + ")");
   System.out.println("stack A: " + A);
   }
  
   static void PushB(Stack<Integer> B, int a) {
   B.push(new Integer(a));
   System.out.println("push(" + a + ")");
   System.out.println("stack B: " + B);
   }
  
   static void MultiPopA(Stack<Integer> A,int sz,int k) {
  
       int p=(sz<k)?sz:k;
       for(int i=p;i>0;i--)
       {
       System.out.print("pop -> ");
       Integer a = (Integer) A.pop();
       System.out.println(a);
       System.out.println("stack A: " + A);
       }
   }
  
   static void MultiPopB(Stack<Integer> B,int sz,int k) {
  
       int p=(sz<k)?sz:k;
       for(int i=p;i>0;i--)
       {
       System.out.print("pop -> ");
       Integer a = (Integer) B.pop();
       System.out.println(a);
       System.out.println("stack B: " + B);
       }
   }
  
   static void Transfer(Stack<Integer> A,Stack<Integer> B,int sza,int szb,int k) {
  
       int p=(sza<k)?sza:k;
       for(int i=p;i>0;i--)
       {
       System.out.print("pop -> ");
       Integer a = (Integer) A.pop();
       System.out.println(a);
       B.push(new Integer(a));
       System.out.println("stack A " + A);
       System.out.println("stack B " + B);
       }
   }
}

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