Question

In: Accounting

a-Assume you have a stack of integers. You want to organize it such that all numbers...

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)

Solutions

Expert Solution

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


Related Solutions

please in java ! Assume you have a stack of integers. The stack contains same number...
please in java ! Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following...
Assume you have a stack of integers. The stack contains same number of positive and negative...
Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following interface: ourStack() constructor, pop,...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
You have n numbers and you want to find a number x (out of the n)...
You have n numbers and you want to find a number x (out of the n) such that x is larger than the median. You can create an algorithim that takes time O(nlogn): sort the n numbers and then report any number that is larger than the element in position n2 of the sorted array. You can also create an algo in O(n) time, by finding the median in linear time and then doing a linear scan to find a...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Show that the integers have infinite index in the additive group of rational numbers.
Show that the integers have infinite index in the additive group of rational numbers.
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
Assume that you have a clock signal with frequency of 50 Hz, and you want to...
Assume that you have a clock signal with frequency of 50 Hz, and you want to divide this signal into a frequency of 1 Hz signal. The counter chips you must use are synchronous decade (MOD 10) counter chips 74160 and synchronous MOD 16 counter chips 74161. Please use the back side of this paper to draw your circuit diagram with all the details. You can use counter frequency division method to step down a higher frequency signal to a...
For exam review: Given a stack S1 with n numbers and an empty stack S2, design...
For exam review: Given a stack S1 with n numbers and an empty stack S2, design an algorithm (and write psudeocode for it) to sort all the numbers (from small on top to large on bottom) and store them in the originally empty stack S2, using only the stack ADT functions with the two given stacks, and a fixed number (independent of n) of int and char variables. What is the time complexity of your algorithm (related to n) in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT