Question

In: Computer Science

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 interface: ourStack() constructor, pop, push, isEmpty, size(), peek()).

B- Solve the same problem using one temporary list with interface: ourList() constructor, insertFront, insertBack, removeFront, removeBack, isEmpty, size().

Bonus Can you solve the problem using one queue (ourQueue with interface (add, remove, isEmpty, size() and peek() that returns the front without removing it).

Big Bonus: Solve the problem using only one temporary stack.

Solutions

Expert Solution

A)Solution:

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
       Stack<Integer> ourStack = new Stack<Integer>();
       Stack<Integer> pos = new Stack<Integer>();
       Stack<Integer> res = new Stack<Integer>();
       ourStack.push(1);
       ourStack.push(3);
       ourStack.push(-2);
       ourStack.push(-8);
       ourStack.push(5);
       ourStack.push(6);
       ourStack.push(-4);
       ourStack.push(-5);
      for(int i=0;i<ourStack.size();i++){
          if(ourStack.get(i)>0){
              pos.push(ourStack.get(i));
          }
      }
      int i=0,j=0;
      while(i<pos.size()&&j<ourStack.size()){
          if(ourStack.get(j)<0){
          res.push(pos.get(i));
          res.push(ourStack.get(j));
          i++;
          }
          j++;
      }
      System.out.println(res);
    }
}

B)SOLUTION:

import java.util.*;
class test{
   public LinkedList<Integer> ourList=new LinkedList<Integer>();
   public test( LinkedList<Integer> ourList){
       this.ourList=ourList;
   }
   public void insertFront(int i){
       ourList.addFirst(i);
   }
   public void insertBack(int i){
       ourList.add(i);
   }
   public void removeFront(){
       ourList.remove(0);
   }
   public void remmoveLast(){
       ourList.remove(ourList.size()-1);
   }
   public boolean isEmpty(){
       return ourList.isEmpty();
   }
   
}
public class MyClass {
    public static void main(String args[]) {
       List<Integer> ourList = new ArrayList<Integer>();
       List<Integer> pos = new ArrayList<Integer>();
       List<Integer> res = new ArrayList<Integer>();
       ourList.add(1);
        ourList.add(3);
        ourList.add(-2);
       ourList.add(-8);
       ourList.add(5);
       ourList.add(6);
       ourList.add(-4);
       ourList.add(-5);
      for(int i=0;i<ourList.size();i++){
          if(ourList.get(i)>0){
              pos.add(ourList.get(i));
          }
      }
      int i=0,j=0;
      while(i<pos.size()&&j<ourList.size()){
          if(ourList.get(j)<0){
          res.add(pos.get(i));
          res.add(ourList.get(j));
          i++;
          }
          j++;
      }
      System.out.println(res);
    }
}

Thank you! if you have any queries post it below in the comment section I will try my best to resolve your queries and I will add it to my answer if required. Please give upvote if you like it.


Related Solutions

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,...
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...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
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...
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
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
2. Answer the following question (a) Write a Java program to find the number of integers...
2. Answer the following question (a) Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number. For example, x = 5, y=20 and p =3, find the number of integers within the range [x, y] and that are divisible by p. (b) Write explicitly on the following OOP concepts: i. Encapsulation ii. Inheritance iii. Polymorphism (c) Develop a Java application that computes the two roots of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT