diff --git a/LinkedStack.java b/LinkedStack.java index a44c080..0d57009 100644 --- a/LinkedStack.java +++ b/LinkedStack.java @@ -3,12 +3,25 @@ * * @author Marcel Turcotte (turcotte@eecs.uottawa.ca) */ +import java.util.Iterator; -public class LinkedStack implements Stack { +public class LinkedStack implements Stack{ // Objects of the class Elem are used to store the elements of the // stack. - + //Don't think these are actually needed, if we do it recursively I think I get it + // private int count = 0; + // private int counter=1; + // LinkedStack a = new LinkedStack(); //this is the original stack +// LinkedStack tmp = new LinkedStack(); + // LinkedStack tmptwo = new LinkedStack(); + //LinkedStack b = new LinkedStack(); //this is the original stack +// LinkedStack temp = new LinkedStack(); + // LinkedStack temptwo= new LinkedStack(); + //LinkedStack tempthree = new LinkedStack(); + E end; + + private int size; private static class Elem { private T value; private Elem next; @@ -46,6 +59,7 @@ public void push(E value) { } top = new Elem(value, top); + size++; } /** Returns the top element, without removing it. @@ -78,23 +92,59 @@ public E pop() { /** Removes the top element of the stack. The element inserted at * the bottom of the stack. */ - + //not sure best way to go about this + //start by poping the top, adding to new temp stack, iterating over remaining stack to add to temp + //then replace old stack with temp stack + public void roll() { - - throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); - + if(top!=null){ + roll(pop());//start recursion + } } + private void roll(E b){ + if (top==null){ + push(b); + }else{ + E temp = pop(); + roll(b); + push(temp); + } + } /** Removes the botttom element. The element is inserted on the * top of the stack. */ - + //ok so make new temp stack, iterate from second to end + //push first onto temp + //replace old with temp + //jk that didn't work I'll try recursion and actually read the instructions f/n/o public void unroll() { - - throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); - + end=null; + if(top!=null){ + E temporary=pop(); + if(top==null){ + push(temporary); + }else{ + push(temporary); + unroller(); + push(end); + } + } } + // E finally; + private void unroller(){ + E now = pop(); + E temporary = pop(); + if(top!=null){ + push(temporary); + unroller(); + push(now); + }else{ + end=temporary; + push(now); + } + } /** Returns a string representation of the stack. * * @return a string representation @@ -116,5 +166,4 @@ public void unroll() { return stackStr.toString(); } - }