Question

In: Computer Science

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, 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.

This question is for my midterm. please I need comments on the code.

the code below is a code that pro covered in class for the stacks and he expect something like that.

public class ourStack<T> {
        private T[]  S;
        private int size = 0;

        public ourStack() {
                S = (T[]) new Object[100];
                size = 0;
        }
        public int size() {
                return size;
        }
        public boolean isEmpty() {
                return (size == 0? true:false);
        }
        //index size is the empty spot. 
        public void push(T e) {
                if(size == S.length)
                        this.resize();
                this.S[size] = e;
                size++;
        }
        public T pop() {
                if(this.isEmpty())
                        return null;
                size--;
                return this.S[size];
        }
        public T peek() {
                if(this.isEmpty())
                        return null;
                return this.S[size - 1];
        }
        private void resize() {
                 //create an array temp of length Ar.length + 100
                 T[] temp = (T[]) new Object[this.S.length+100];
                 //copy array Ar into array temp
                 for(int i = 0; i<S.length; i++)
                         temp[i] = this.S[i];
                 //assign temp to Ar.
                 this.S = temp;
                 
         }
        //write a toString to return the content of the array in the form  [-3, -2, -5]
        public String toString() {
                String st = "[";
                if(this.isEmpty())
                        return "[]";
                 for (int i = 0; i<size -1; i++) {
                         st += this.S[i] + ", ";
                 }
                 st += this.S[size - 1] + "]";   
                
                 return st;      
                         
                 }      

}

Solutions

Expert Solution

Of all the ways to solve this problem, I went for the most optimized one so that you can get that BIG BONUS. I used the stack class provided in the question for stack functions.

import java.util.*;
import java.io.*;

public class Main{
  public static void main(String[] args){
    ourStack<Integer> s1 = new ourStack<Integer>();
    s1.push(5);
    s1.push(3);
    s1.push(2);
    s1.push(-1);
    s1.push(-6);
    s1.push(8);
    s1.push(-9);
    s1.push(-4);

    ourStack<Integer> s2 = new ourStack<Integer>();
    int a=s1.peek();
    while(s1.isEmpty()==true || s2.isEmpty()==true){
    while(s1.size()>0){
      if(s1.peek()>0 && a>0){
        a=s1.pop();
        s2.push(a);
      }else if(s1.peek()<0 && a<0){
        a=s1.pop();
        s2.push(a);
      }else{
        a=s1.pop();
        System.out.print(a+" ");
      }
    }
    
    while(s2.size()>0){
      if(s2.peek()>0 && a>0){
        a=s2.pop();
        s1.push(a);
      }else if(s2.peek()<0 && a<0){
        a=s2.pop();
        s1.push(a);
      }else{
        a=s2.pop();
        System.out.print(a+" ");
      }
    }
  }
  }
}

(Throw an upvote or comment if you have any doubts.)


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...
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...
Suppose that two stacks of positive integers are needed for a project. Each stack is to...
Suppose that two stacks of positive integers are needed for a project. Each stack is to contain integers that are less than or equal to 500. One stack is to contain even integers; the other stack is to contain odd integers. Also, the total number of values in the combined stacks at any given time will not exceed 200. ▪ Design a way to implement both stacks in only one(pay attention!!!) 1-D array. ▪ Write a Python class definition(s) for...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or zero. Write pseudocode for a program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5). Notes: 1. Try working this out for k = 4 and k = 5. See if you can find an algorithmic pattern that can be extended for...
For example, suppose you had a program to determine if a number is positive, negative or zero.
  For example, suppose you had a program to determine if a number is positive, negative or zero. Read in a number and then use the if statement to determine if the number is positive (>0) or negative (<0) or zero (==0): if (x>0)//display positive messageelse if (x<0)   //display negative messageelse//display zero message
Consider all positive integers less than 100. Find the number of integers divisible by 3 or...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or 5? Consider strings formed from the 26 English letters. How many strings are there of length 5? How many ways are there to arrange the letters `a',`b', `c', `d', and `e' such that `a' is not immediately followed by`e' (no repeats since it is an arrangement)?
Consider an array of length n containing positive and negative integers in random order. Write the...
Consider an array of length n containing positive and negative integers in random order. Write the C++ code that rearranges the integers so that the negative integers appear before the positive integers. write a program that includes both functions and a main() function that tests them. Name the two functions rearrangeN() and rearrangeN2().
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...
1a. What is the largest positive and negative number that you could represent with 7 bits...
1a. What is the largest positive and negative number that you could represent with 7 bits using signed magnitude? Show your work. 1c. Solve the following decimal notation equation using 8-bit binary numbers and 2’s complement notation: 69 - 7 = Show your work
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT