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

I\'m going to need help trying to this this method Write a method splitStack tha

ID: 3839267 • Letter: I

Question

I'm going to need help trying to this this method

Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then get all the negative numbers. It does not matter what order the numbers appear in as long as all the negatives appear lower in the stack than all the non-negatives. You may use a single queue as auxiliary storage. For example, if the stack stores [3, -5, 1, 2, -4] , an acceptable result would be [-5, -4 , 3, 1, 2]

Explanation / Answer

StackofNumbers.java:

import java.util.Iterator;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

import java.util.Stack;

public class StackofNumbers {

            public static void main(String[] args) {

                        Scanner sc = new Scanner(System.in);

                        Stack<Integer> stack = new Stack<Integer>();

                        System.out.println("Enter number of elements:");

                        int n = sc.nextInt();

                        System.out.println("Enter stack elements:");

                        for(int i=0;i<n;i++)

                                    stack.push(sc.nextInt());

                        System.out.println("Stack elements: "+stack);

                        stack = splitStack(stack);

                        System.out.println("Stack elements after spliting: "+stack);

            }

            public static Stack<Integer> splitStack(Stack<Integer> stack){

                        Queue<Integer> queue = new LinkedList<Integer>();

                        Iterator<Integer> ie = stack.iterator();

                        for(int i=0;i<stack.size();i++){

                                    int num = stack.get(i);

                                    if(num<0)

                                                queue.add(num);

                        }

                        while(ie.hasNext()){

                                    int num = ie.next();

                                    if(num>0)

                                                queue.add(num);

                        }

                        stack.removeAllElements();

                        stack.addAll(queue);

                        return stack;

            }

}

Sample Input and output:

Enter number of elements:

5

Enter stack elements:

3 -5 1 2 -4

Stack elements: [3, -5, 1, 2, -4]

Stack elements after spliting: [-5, -4, 3, 1, 2]

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