0
import java.util.NoSuchElementException;
import java.util.Stack;
public  class StackList<T> implements SomeList<E>{
    /* Comments
     * only use offer(), poll()/remove, isEmpty(), and size() methods*/

    private Stack<T> stack=new Stack<T>();
    public StackList(){
        Stack<T> stack=new Stack<T>();
        stack=stack1;
    }

    public void push(T thing){
        stack1.push(thing);
    }
    @Override
    public T pop(){
        if(this.isEmpty()){throw new StackOverflowError();}
        return stack1.pop();
    }
    @Override
    public void add(T thing) {
        stack1.push(thing);
    }
    public void addFront(T thing) {
        int size=this.size();
        T[] arr=(T[]) new Object[size];
        int i=0;
        while(!this.isEmpty()){arr[i]=this.pop();i++;}
    }
    public T remove(int index) {
        if(index>this.size()){throw new IndexOutOfBoundsException();}
        else{int sze=this.size();
            T[] arr1=(T[])new Object[this.size()];
            int i=0;
            while(!this.isEmpty()){arr1[i]=this.pop();i++;}
            T vre = arr1[index];
            for(int j=0;j<size;j++){this.push(arr1[i]);}
            return vre;
        } 
    }
    public E removeEnd() {
        if(this.isEmpty()){return null;}
        return this.pop();
    }
    public E get(int index) {int sze=this.size();
        if(index>sze){throw new NoSuchElementException();}
        else{
            T[] arr2=(T[]) new Object[sze];
            int i=0;
            while(!this.isEmpty()){arr2[i]=this.pop();i++;}
            for(int j=0;j<sze;j++){this.push(arr2[j]);}
            return arr2[index];
        }
    }
    public int size() {
        return this.size();
    }
    public boolean isEmpty() {
        return this.isEmpty();
    }
}

Edited by pritaeas: Moved to Java.

2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by JamesCherrill
0

Normally I would say " go ahead and test it, you'll soons ee if it's ok". But in this case you have a couple of catastrphic mistakes that will prevent any useful testing. You have 2 methods that look something like

int doIt() {
   return doIt();
}

These will call themselves recursively until you get a stack overflow.

Normally methods like that simply return a (private) local variable, or some result derived from local variables.

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.