Question
Program in Netbeans
Write a method Stack Merge (Stack p1, Stack p2) to
0 0
Add a comment Improve this question Transcribed image text
Answer #1


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


public class StackMerging {

   public static void main(String[] args) {
      
       int count1,count2;
      
       Stack<Integer> p1 = new Stack<Integer>(); // Instance of Stack P1
       Stack<Integer> p2 = new Stack<Integer>(); // Instance of Stack P1
       Stack<Integer> p0 = new Stack<Integer>(); // Instance of Stack P1
      
       Scanner s = new Scanner(System.in);
      
       System.out.println("Enter no. of elements of stack1:");
       count1 = s.nextInt();
      
       System.out.println("Enter no. of elements of stack2:");
       count2 = s.nextInt();
      

      
       System.out.println("Enter elements into stack1:");
       for (int i=0;i<count1;i++) {
           p1.push(s.nextInt()); // Pushing into Stack
       }
      
      
       System.out.println("Enter elements into stack2:");
       for (int i=0;i<count2;i++) {
           p2.push(s.nextInt()); // Pushing into Stack
       }
       System.out.println("Elements of p1 before merging:"+p1);
       System.out.println("Elements of p2 before merging:"+p2);

       p0 = stackMerge(p1,p2); // static method for Merging p1,p2 stacks
      
       System.out.println("Elements of p0 after merging is:"+p0);
      
   }

   private static Stack<Integer> stackMerge(Stack<Integer> p1,
           Stack<Integer> p2) {

       Stack<Integer> p0 = new Stack<Integer>();
      
       int i=0,j=0;
       while(i<p1.size()||j<p2.size()){ // Pushing into Stack P0 by retrieving elements from P1 and P2
           if(i<p1.size()){
               p0.push(p1.get(i));
               i++;
           }
           else{
               if(!p0.contains(p2.get(j))) // Removing duplicates by checking in the P0
                    p0.push(p2.get(j));
               j++;
           }
       }
       return p0;
   }

}

Add a comment
Know the answer?
Add Answer to:
Program in Netbeans Write a method Stack Merge (Stack p1, Stack p2) to return a stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in...

    Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in Stack class and Queue interface. The Stack class is a generic class containing these methods: public void push(E value) - pushes a new value onto the Stack public E pop() - pops the next value off of the stack and returns it; throws EmptyStackExceptionl if stack is empty public E peek() - returns the next value on the Stack but does not pop it...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public...

    JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public E peek() { return getList().get(indexOfTop()); } public E pop() { return getList().remove(indexOfTop()); } public Stack push(E pData) { getList().append(pData); return this; } private DList getList() { return mList; } private void setList(DList pList) { mList = pList; } Q1 Because Stack encapsulates an instance variable which is of the class DList,...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT