Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions LinkedStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
*
* @author Marcel Turcotte ([email protected])
*/
import java.util.Iterator;

public class LinkedStack<E> implements Stack<E> {
public class LinkedStack<E> implements Stack<E>{

// 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<E> a = new LinkedStack<E>(); //this is the original stack
// LinkedStack<E> tmp = new LinkedStack<E>();
// LinkedStack<E> tmptwo = new LinkedStack<E>();
//LinkedStack<E> b = new LinkedStack<E>(); //this is the original stack
// LinkedStack<E> temp = new LinkedStack<E>();
// LinkedStack<E> temptwo= new LinkedStack<E>();
//LinkedStack<E> tempthree = new LinkedStack<E>();
E end;

private int size;
private static class Elem<T> {
private T value;
private Elem<T> next;
Expand Down Expand Up @@ -46,6 +59,7 @@ public void push(E value) {
}

top = new Elem<E>(value, top);
size++;
}

/** Returns the top element, without removing it.
Expand Down Expand Up @@ -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
Expand All @@ -116,5 +166,4 @@ public void unroll() {

return stackStr.toString();
}

}