NumberStack .java
public class NumberStack {
// To define the SIZEimum size of the Stack
static final int SIZE = 500;
int stack[] = new int[SIZE];
int top; // Top of the Stack , Initially will be
-1
// Default constructor
NumberStack() {
top = -1; // initial value
}
// This method will be used to push an element at
the top of the Stack
// If Stack is full then error is printed.
// returns boolean
boolean push(int element) {
if (top >= SIZE) {
System.out.println("************************Stack
Overflow************************");
return
false;
} else // Insertion is
successful
{
stack[++top] =
element;
return
true;
}
}
// This method will be used to pop an element at
the top of the Stack
// If Stack is empty then error is printed.
// returns boolean
int pop() {
if (isEmpty()) {
System.out.println("********************************Stack
Underflow**********************");
return 0;
} else { // Element is taken and
value is returned
int element =
stack[top--];
return
element;
}
}
// To Check if the stack is empty
boolean isEmpty() {
return (top < 0);
}
}
RemoveMin .java
public class RemoveMin {
static NumberStack stack = new NumberStack();
public static void main(String[] args) {
// First add some elements to stack then test the methods
stack.push(2);
stack.push(8);
stack.push(3);
stack.push(19);
stack.push(7);
stack.push(3);
stack.push(2);
stack.push(3);
stack.push(2);
stack.push(7);
stack.push(12);
stack.push(-8);
stack.push(4);
//call method removeMin
int minimum=removeMin(stack);
System.out.println("Minimum Value
Removed is = "+minimum);
//Refill Stack
stack.push(2);
stack.push(8);
stack.push(3);
stack.push(19);
stack.push(7);
stack.push(3);
stack.push(2);
stack.push(3);
stack.push(2);
stack.push(7);
stack.push(12);
stack.push(4);
int
minimum1=removeMin(stack);
System.out.println("Minimum Value
Removed is = "+minimum1);
}
/***
*
* @param s
* @return
*/
public static int removeMin(NumberStack s) {
int min = 0;
// First make a copy of original
stack , so that
if (s.top < 0) {
return 0;
}
else {
// Convert
the stack to array ,
// find minimum
values, Rebuild Stack
// top will
be maximum index of the array
int size =
s.top;
int array[] =
new int[size + 1];
while (size
>= 0) // filling array
{
array[size] = s.pop();
size--;
}
// find
min
min =
array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
// Rebuild
the array
int[]
changedArray = new int[array.length - 1];
for (int i = 0, j = 0; i < array.length; i++) {
if (array[i] == min) {
continue;
}
changedArray[j++] = array[i];
}
// Now rebuild the Stack from changedArray
for (int i =
changedArray.length - 1; i >= 0; i--) {
stack.push(changedArray[i]);
}
//Print the
stack again
System.out.println("***************Stack Elements After Removing
Minimum are : ***********************");
while
(!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
return min;
}
}
Output
***************Stack Elements After Removing Minimum are :
***********************
2
8
3
19
7
3
2
3
2
7
12
4
Minimum Value Removed is = -8
***************Stack Elements After Removing Minimum are :
***********************
8
3
19
7
3
3
7
12
4
0
0
Minimum Value Removed is = 2
n: Please use comment on the top of your program to record: starting and completion time....
) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and clear. Complete the two unfinished methods. Do not modify any other parts of the class. // Looks at the top two elements of the stack, and removes and returns the larger // of the two elements from the stack, returning the other element to the stack. // For example, if the stack, from the top, is 8 10 7 2...
Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...
NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...
Need some help with this java problem, thanks!
Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Write a program that prints out the lists alter the following methods are called. Method takes an of integers as a parameter. The method moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called list stores the following values: [4. 7, 9, 2. 7, 7. 5....
Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter and reverses only the values in the bottom half of the Stack. For example, if a Stack containing the values [1, 2, 3, 4, 5] were passed in (with 1 at the bottom and 5 at the top), the Stack would be changed to [2, 1, 3, 4, 5] (with 2 at the bottom and 5 at the top) after this method was called...
In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). Do NOT change your class name from "Main". The Main class should come first in your code. Your filename should be "Main.java". The Queue3503 class will contain:...
In java please: Write a method that accepts as a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally. Only use a single stack as auxiliary storage. For example, if a queue variable named q stores the following elements: front {1, -2, 4, 5, -7, -9, -12, 28, -34} back Then the call of reorder(q); should modify it to store the following values: front {-34, -12, -9,...
In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list of Integer. The start of the linked list is referenced by values which, of course, is of type LLNode. For example purposes, assume values points to a list containing 3, 6, 6, 9, 12, 15, 18, 19, 19, and 20. You can and should assume the list in question is sorted in nondecreasing order. For each of these exercises you should also create a...
JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...
JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...