In: Accounting
a-Assume you have a stack of integers. You want to organize it such that all numbers that are smaller than -100 go to the bottom, numbers between -100 and +100 in the middle and numbers larger than 100 in the top. Write a Java code that uses no more than three additional Stacks to solve the problem. NB: You do not need to write the code for Stacks, you are using a Stack from the library with a name SStack that exposes the following interface: SStack() constructor, pop, push, isEmpty, size(), peek()). 2- Solve the same problem as above using one stack and one queue (or two queues), with interface: SQueue() constructor, insert, remove, isEmpty, size()
b-Can you solve the problem using one list with an interface (insertFront, insertBack, removeFront, removeBack, isEmpty, size(), and insertAt(int index, int num) that inserts at specific index)
SOLUTION:-
CODE:- JAVA
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class OrganizeIntegers
{
Stack<Integer> stack1,stack2,stack3; // using 3 additional
stacks
Queue<Integer> queue1,queue2; // using 2 additional
queues
public OrganizeIntegers()
{
stack1=new Stack<>();
stack2=new Stack<>();
stack3=new Stack<>();
queue1=new LinkedList<>();
queue2=new LinkedList<>();
}
void usingStack(Stack<Integer> stack)
{
while(!stack.empty())
{
int i=stack.pop();
if(i<-100)
{
stack1.push(i); // stack1 with int<-100(bottom)
}
else if(i>100)
{
stack3.push(i); // stack3 with int>100(top)
}
else
{
stack2.push(i); //stack2 with int between 100 n -100
}
}
while(!stack1.empty())
{
int i=stack1.pop();
stack.push(i);
}
while(!stack2.empty())
{
int i=stack2.pop();
stack.push(i);
}
while(!stack3.empty())
{
int i=stack3.pop();
stack.push(i);
}
display(stack);
}
void usingQueue(Stack<Integer> stack)
{
while(!stack.empty())
{
int i=stack.pop();
queue1.add(i);
}
int size=queue1.size();
while(size!=0)
{
int i=queue1.remove();
if(i<-100)
{
stack.push(i); // will take bottom int<-100
}
else if(i>100)
{
queue1.add(i); // queue1 contains int>100 after all elements
processed
}
else
{
queue2.add(i); //queue2 has integers between 100 n -100
}
size--;
}
while(queue2.size()!=0)
{
stack.push(queue2.remove());
}
while(queue1.size()!=0)
{
stack.push(queue1.remove());
}
display(stack);
}
void display(Stack<Integer> stack)
{
System.out.println("Stack Elements: ");
while(!stack.empty())
{
int i=stack.pop();
System.out.println(i+" ");
}
}
public static void main(String[] args)
{
Stack<Integer> stack=new Stack<>(); // stack with
integers for stack problem
Stack<Integer> stackq=new Stack<>(); //stack with
integers for queue problem
//integers for stack problem
stack.push(-190);
stack.push(-100);
stack.push(190);
stack.push(-200);
stack.push(200);
stack.push(100);
stack.push(-485);
stack.push(485);
stackq.push(-67);
// integers for queue problem
stackq.push(190);
stackq.push(45);
stackq.push(-160);
stackq.push(-2090);
stackq.push(-78);
stackq.push(280);
stackq.push(90);
stackq.push(-685);
stackq.push(405);
OrganizeIntegers obj=new OrganizeIntegers();
obj.usingStack(stack); // arrange integers using stack
obj.usingQueue(stackq); // arrange integers using queue
}
}
THANK YOU , If any queries please leave your valuable comment on comment box....
If possible than rate the answer as well...Thumbs up
Type a message