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...
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...
Java, no imports: Given a list of integers, round each number to the nearest multiple of...
Java, no imports: Given a list of integers, round each number to the nearest multiple of 5.        2.5-7.49 round to 5. 7.5-12.49 round to 10. 12.5-17.49 round to 15. etc.        return the sum of all the rounded elements.        Implement this way for credit:        Fill in the method directly below this one called helperRound() to round each number. Call that method and use the sum of the returned values.        helperSum({4.3, 16.7})...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT