In: Computer Science
*You can still get part marks without correctly implementing the iterator() method. But for full marks, the iterator() method needs to return an Object that supports the next() and hasNext() methods from the Iterator<T> interface. For a MinStack, the iterator should output the elements at the bottom of the stack first (the elements that have been in the stack the longest), finishing with the element at the top of the stack (the element that would be returned by a call to pop()). For a MinDeque, the iterator should output the elements beginning with the first element (the one that would be returned by removeFront()) and finishing with the last element (the one that would be returned by removeLast()).
Hint: There's no need to build your iterators from scratch. The collections in java.util all have iterators.
package comp2402a2;
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class FastMinStack<T> implements MinStack<T> {
protected Comparator<? super T> comp;
public FastMinStack() {
this(new DefaultComparator<T>());
}
public FastMinStack(Comparator<? super T> comp) {
this.comp = comp;
// TODO: Your code goes here
}
public void push(T x) {
// TODO: Your code goes here
}
public T pop() {
// TODO: Your code goes here
return null;
}
public T min() {
// TODO: Your code goes here
return null;
}
public int size() {
// TODO: Your code goes here
return -1;
}
public Iterator<T> iterator() {
// TODO: Your code goes here
return null;
}
public Comparator<? super T> comparator() {
return comp;
}
}
package comp2402a2;
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class FastMinDeque<T> implements MinDeque<T> {
protected Comparator<? super T> comp;
public FastMinDeque() {
this(new DefaultComparator<T>());
}
public FastMinDeque(Comparator<? super T> comp) {
this.comp = comp;
// TODO: Your code goes here
}
public void addFirst(T x) {
// TODO: Your code goes here
}
public void addLast(T x) {
// TODO: Your code goes here
}
public T removeFirst() {
// TODO: Your code goes here
return null;
}
public T removeLast() {
// TODO: Your code goes here
return null;
}
public T min() {
// TODO: Your code goes here
return null;
}
public int size() {
// TODO: Your code goes here
return -1;
}
public Iterator<T> iterator() {
// TODO: Your code goes here
return null;
}
public Comparator<? super T> comparator() {
return comp;
}
}
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class FastMinStack<T> implements MinStack<T> {
protected Comparator<? super T> comp;
// we will take two linked list ds will hold our original value
// and auxiliary will store the minimum value till now.
protected List<T> ds;
protected List<T> auxiliary;
public FastMinStack() {
this(new DefaultComparator<T>());
}
public FastMinStack(Comparator<? super T> comp) {
this.comp = comp;
ds=new LinkedList<T>();
auxiliary=new LinkedList<T>();
}
public void push(T x) {
// TODO: Your code goes here
// when we push any element we will see that if stack is empty then insert
// x into both ds and auxiliary list
// if stack is not empty then compare the top value from auxiliary list
// with x if x is smaller then add it to auxiliary list also
if(ds.size()==0)
{
ds.add(x);
auxiliary.add(x);
return ;
}
T y=auxiliary.get(auxiliary.size()-1);
if(comp.compare(x, y)<0)
{
auxiliary.add(x);
}
}
public T pop() {
// TODO: Your code goes here
// while poping we will see that if poped element is also top element of the
// auxiliary list then also removed from auxiliary list.
if(size()==0)
return null;
T x=ds.remove(size()-1);
if(comp.compare(x, auxiliary.get(size()-1))==0)
{
auxiliary.remove(auxiliary.size()-1);
}
return null;
}
public T min() {
if(size()==0)
return null;
return auxiliary.get(auxiliary.size()-1);
}
public int size() {
// TODO: Your code goes here
return ds.size();
}
public Iterator<T> iterator() {
// TODO: Your code goes here
return ds.iterator();
}
public Comparator<? super T> comparator() {
return comp;
}
}