From f92b348b9a0ee0897e6400e60f245192dda00fb0 Mon Sep 17 00:00:00 2001 From: Doyle1771 Date: Wed, 5 Apr 2017 21:29:01 -0400 Subject: [PATCH 1/2] Update LinkedStack.java --- LinkedStack.java | 77 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/LinkedStack.java b/LinkedStack.java index a44c080..d0ab481 100644 --- a/LinkedStack.java +++ b/LinkedStack.java @@ -3,8 +3,9 @@ * * @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. @@ -78,21 +79,65 @@ 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 + //NONE OF THIS WORKS JUST IGNORE IT FOR NOW ILL FIGURE IT OUT EVENTUALLY public void roll() { - - throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); - + LinkedStack a = new LinkedStack(); //this is the original stack + a=this; + LinkedStack tmp = new LinkedStack(); //this will be the new stack + LinkedStack tmptwo = new LinkedStack(); + + if(top==null){ + throw new UnsupportedOperationException("Invalid operation for linked stack. Method roll."); + }else{ + if(tmptwo==null){//if tmp is empty then take the top element of a and make it the bottom element of tmp + tmptwo.push(a.pop()); + }else{//once that element has been added, want to go through a from bottom to top + while(top!=null){ + tmp.push(a.pop()); + } + } + } + tmp.push(tmptwo.pop()); + a=tmp; + tmp=null; + tmptwo=null; } /** 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 public void unroll() { - - throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); - + LinkedStack a = new LinkedStack(); //this is the original stack + a=this; + LinkedStack tmp = new LinkedStack(); //this will be the new stack + LinkedStack tmptwo = new LinkedStack(); + LinkedStack tmpthree = new LinkedStack(); + if (top==null){ + throw new UnsupportedOperationException("Invalid operation for linked stack. Method unroll."); + }else{ + int count = 0; + while(top!=null){ + tmp.push(a.pop()); + } + if (tmptwo==null && count == 0){ + tmptwo.push(tmp.pop()); + count = 1; + } + tmpthree.push(tmptwo.pop()); + while(tmp!=null){ + tmpthree.push(tmp.pop()); + } + } + a=tmpthree; + tmp=null; + tmptwo=null; + tmpthree=null; } /** Returns a string representation of the stack. @@ -116,5 +161,17 @@ public void unroll() { return stackStr.toString(); } - + public static void main (String args[]){ + Stack s ; + s = new LinkedStack(); + s.push("a"); + s.push("b"); + s.push("c"); + s.push("d"); + s.push("e"); + System.out.println(s); + s.roll(); + s.roll(); + System.out.println(s); + } } From 7be6eac55c064437a186fbe44cccc50f04229ac5 Mon Sep 17 00:00:00 2001 From: Doyle1771 Date: Mon, 10 Apr 2017 02:24:59 -0400 Subject: [PATCH 2/2] Update LinkedStack.java --- LinkedStack.java | 104 ++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/LinkedStack.java b/LinkedStack.java index d0ab481..0d57009 100644 --- a/LinkedStack.java +++ b/LinkedStack.java @@ -9,7 +9,19 @@ 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; @@ -47,6 +59,7 @@ public void push(E value) { } top = new Elem(value, top); + size++; } /** Returns the top element, without removing it. @@ -82,64 +95,56 @@ public E pop() { //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 - //NONE OF THIS WORKS JUST IGNORE IT FOR NOW ILL FIGURE IT OUT EVENTUALLY + public void roll() { - LinkedStack a = new LinkedStack(); //this is the original stack - a=this; - LinkedStack tmp = new LinkedStack(); //this will be the new stack - LinkedStack tmptwo = new LinkedStack(); + if(top!=null){ + roll(pop());//start recursion + } + } - if(top==null){ - throw new UnsupportedOperationException("Invalid operation for linked stack. Method roll."); + private void roll(E b){ + if (top==null){ + push(b); }else{ - if(tmptwo==null){//if tmp is empty then take the top element of a and make it the bottom element of tmp - tmptwo.push(a.pop()); - }else{//once that element has been added, want to go through a from bottom to top - while(top!=null){ - tmp.push(a.pop()); - } - } + E temp = pop(); + roll(b); + push(temp); } - tmp.push(tmptwo.pop()); - a=tmp; - tmp=null; - tmptwo=null; } - /** 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() { - LinkedStack a = new LinkedStack(); //this is the original stack - a=this; - LinkedStack tmp = new LinkedStack(); //this will be the new stack - LinkedStack tmptwo = new LinkedStack(); - LinkedStack tmpthree = new LinkedStack(); - if (top==null){ - throw new UnsupportedOperationException("Invalid operation for linked stack. Method unroll."); - }else{ - int count = 0; - while(top!=null){ - tmp.push(a.pop()); - } - if (tmptwo==null && count == 0){ - tmptwo.push(tmp.pop()); - count = 1; - } - tmpthree.push(tmptwo.pop()); - while(tmp!=null){ - tmpthree.push(tmp.pop()); + end=null; + if(top!=null){ + E temporary=pop(); + if(top==null){ + push(temporary); + }else{ + push(temporary); + unroller(); + push(end); } } - a=tmpthree; - tmp=null; - tmptwo=null; - tmpthree=null; } + // 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 @@ -161,17 +166,4 @@ public void unroll() { return stackStr.toString(); } - public static void main (String args[]){ - Stack s ; - s = new LinkedStack(); - s.push("a"); - s.push("b"); - s.push("c"); - s.push("d"); - s.push("e"); - System.out.println(s); - s.roll(); - s.roll(); - System.out.println(s); - } }