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 positives. ***YOU MUST USE A QUEUE AS AN AUXILIARY STORAGE***
Complete JAVA program to demonstrate divideStack method:-
import java.util.Queue;
import java.util.Stack;
import java.util.LinkedList;
class Main {
// Method to divide stack into positives and negatives using a queue
public static void divideStack(Stack<Integer> st){
Queue<Integer> q = new LinkedList<Integer>();
int numOfNegative = 0;// Number of negative numbers in the stack
// Loop till stack becomes empty
while(!st.isEmpty()){
int val = st.pop();// Pop a value from the stack
if(val < 0)// If popped value is negative
numOfNegative++;
q.add(val);// Insert value into the queue
}
while(numOfNegative > 0){
int val = q.remove();// Remove a value from the queue
if(val < 0){// If value is negative
st.push(val);// Push negative values first
numOfNegative--;
}
else
q.add(val);// Insert positive value back in the queue
}
// Now, only positive values are remaining in the queue
// Push all positive values into the stack
while(!q.isEmpty()){
int val = q.remove();// Remove a value from the queue
st.push(val);// Push positive value into the stack
}
}
// Main method to demonstrate divideStack method
public static void main(String[] args) {
Stack<Integer> st = new Stack<Integer>();// Create a stack
// Push values into the stack
st.push(-5);
st.push(4);
st.push(6);
st.push(-9);
st.push(-7);
st.push(11);
st.push(-12);
System.out.println("Stack before calling divideStack(): " + st);
divideStack(st);
System.out.println("Stack after calling divideStack(): " + st);
}
}
OUTPUT:

FOR ANY HELP JUST DROP A COMMENT
PLEASE DONT COPY PASTE EXISTING SOLUTIONS Practice problem on stacks and queues. Thanks :) Write a...
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...
Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...
Overview For this assignment, we will practice using stacks and queues. In this exercise, you will create two stacks and a queue to keep track of cars using a parking lot. We will use the nature of the stacks and queues to solve the problem. Specifications Rules FIU has opened a new valet parking lot next to the PC building that requires a special decal to park. If the car does not have the correct parking decal, the driver should...
This project will allow you to write a program to get more practice with the stack and queue data structures, as well as more practice with object-oriented ideas that we explored in the previous projects. In this assignment you will be writing a simulation of an order-fulfillment system for a company like Amazon.com. These companies take orders for products and ship them to customers based on what they have in inventory. For this assignment you will be performing a scaled-back...