In: Computer Science
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class.
import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super T> comp) { this.comp = comp; ds = new LinkedList<T>(); } public void push(T x) { } public T pop() { } public T getMin() { } public int size() { return ds.size(); } public Iterator<T> iterator() { } public Comparator<? super T> comparator() { return comp; } }
Classes it extends to:
import java.util.Comparator; public interface MinStack<T> extends Iterable<T> { public Comparator<? super T> comparator(); public void push(T x); public T pop(); public T getMin(); public int size(); }
And
import java.util.Comparator; class DefaultComparator<T> implements Comparator<T> { @SuppressWarnings("unchecked") public int compare(T a, T b) { return ((Comparable<T>)a).compareTo(b); } }
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Since you mentioned that you already have a test program, I am not including any.
//FMinStack.java
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Iterator;
public class FMinStack<T> implements MinStack<T> {
protected Comparator<? super T> comp;
protected LinkedList<T> ds;
// a linked list to store minimum values
protected LinkedList<T> minValues;
public FMinStack() {
this(new DefaultComparator<T>());
}
public FMinStack(Comparator<? super T> comp) {
this.comp = comp;
ds = new LinkedList<T>();
// also initializing minValues
minValues = new LinkedList<T>();
}
public void push(T x) {
// adding x to the head of ds
ds.addFirst(x);
// if minValues is empty, or if x<head value of minValues, adding x to
// the head of minValues
if (minValues.isEmpty() || comp.compare(x, minValues.getFirst()) <= 0) {
minValues.addFirst(x);
}
// otherwise adding the same head value to the head of minValues
else {
minValues.addFirst(minValues.getFirst());
}
// the idea is that, at any point, the first element in minValues point
// to the current smallest element CURRENTLY on the stack.
}
public T pop() {
if (size() > 0) {
// removing front/head values from ds and minValues. so minValues
// will now point to the next smallest value available on the stack.
T top = ds.removeFirst();
minValues.removeFirst();
return top;
}
return null; //empty
}
public T getMin() {
if (size() > 0) {
//returning value at the head of minValues
return minValues.getFirst();
}
return null;
}
public int size() {
return ds.size();
}
public Iterator<T> iterator() {
return ds.iterator();
}
public Comparator<? super T> comparator() {
return comp;
}
}