n: Please use comment on the top of your program to record: starting and complet
ID: 3706161 • Letter: N
Question
n: Please use comment on the top of your program to record: starting and completion time. Design a stack named NumberStack. java which is used to store integers (you may want to refer to the Numberstack.java in HW03). Create another file named RemoveMin. java and implement the method as specified below: public static int removeMin(NumberStack s) This method accepts a Numberstack as a parameter and removes and returns the smallest value from the stack. For example if a variable s stores these values: bottom [2, 8, 3, 19, 7, 3, 2, 3, 2, 7, 12, 8, 4] top and we make the following call: int n-removeMin(s); The method removes and returns -3, so n will store -8 after the call and s will store the following values: bottom [2, 8, 3, 19, 7, 3, 2, 3, 2, 7, 12, 4] top If the minimum value appears more than once, all occurrences of it should be removed. For example, given the stack above, if we again call removeMin(s), it would return 2 and leave the stack as follows: bottom [8, 3, 19, 7, 3, 3, 7, 12, 4] topExplanation / Answer
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.