Question

Page ot 9 2. Stacks/Queues: 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this method. Assuming the language is Java. Please don’t forget to mention the language while you post a question in future. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//required method, using Stack & Queue from java.util library

static void splitStack(Stack<Integer> stk) {

      // creating a queue as auxiliary storage

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

      // moving each value from stk to the queue

      while (!stk.isEmpty()) {

            auxQueue.add(stk.pop());

      }

      // finding the size of the queue

      int size = auxQueue.size();

      // looping for size number of times

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

            // removing front element from queue

            int number = auxQueue.remove();

            // if number is negative, pushing to stack

            if (number < 0) {

                  stk.push(number);

            } else {

                  // otherwise adding to the end of queue

                  auxQueue.add(number);

            }

      }

      // at this point, the stack will only contain negative values, all

      // positive values are left on the queue, so we just simply add them to

      // the stack, so that positive values will be at the top

     

      // looping and removing each value remaining on the queue into the stack

      while (!auxQueue.isEmpty()) {

            stk.push(auxQueue.remove());

      }

}

Add a comment
Know the answer?
Add Answer to:
Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as...
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
  • PLEASE DONT COPY PASTE EXISTING SOLUTIONS Practice problem on stacks and queues. Thanks :) Write a...

    PLEASE DONT COPY PASTE EXISTING SOLUTIONS Practice problem on stacks and queues. Thanks :) Write a method called divideStack that takes a stack of integers as a parameter and divides it into negatives and positives.The numbers in the stack must be arranged so that all negatives appear on the bottom of the stack and all positives appear on the top. The order of the numbers does not matter as long as all negatives appear lower in the stack than all...

  • 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...

  • In java please: Write a method that accepts as a parameter a queue of integers that...

    In java please: Write a method that accepts as a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally. Only use a single stack as auxiliary storage. For example, if a queue variable named q stores the following elements: front {1, -2, 4, 5, -7, -9, -12, 28, -34} back Then the call of reorder(q); should modify it to store the following values: front {-34, -12, -9,...

  • Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter...

    Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter and reverses only the values in the bottom half of the Stack. For example, if a Stack containing the values [1, 2, 3, 4, 5] were passed in (with 1 at the bottom and 5 at the top), the Stack would be changed to [2, 1, 3, 4, 5] (with 2 at the bottom and 5 at the top) after this method was called...

  • Data Structures and Algorithm (Stacks and Queues) Imagine you have a Queue of integers, Q1 and...

    Data Structures and Algorithm (Stacks and Queues) Imagine you have a Queue of integers, Q1 and Q2. Draw a picture of Q1 and Q2 after the following operations: **Note: make sure that ALL variables are simulated.** Data are: 5,7,12,4,0,4,6 (these are the numbers) Algorithm: 1 loop (not end of the file) 1read number 2enqueue(Q1,number) 3enqueue(Q2,number) 4loop(not empty Q1) 1dequeue(Q1,x) 2enqueue(Q2,x) Send loop 2end loop

  • Write a method called reverseFirstK that accepts an integer k and a queue of integers as...

    Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...

  • help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow...

    help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Exercises: 1) Which of the following applications may use a stack? A. parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above. 2) Consider the usual algorithm for determining whether a sequence of parentheses is balanced....

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

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