Reputation: 125
I am creating a linked list implementation for a stack. I've got the pop and push method done, but I can't seem to get the peek methods right. The code I have now in there, returns the memory address I think.
Here is my code:
public class LinkedStack<T> implements StackADT<T> {
private int count;
private LinearNode<T> contents;
public LinkedStack() {
count = 0;
contents = null;
}
@Override
public void push(T element) {
LinearNode<T> top = new LinearNode<T>(element);
if (contents == null) {
contents = top;
} else {
LinearNode<T> current = contents;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(top);
}
count++;
}
@SuppressWarnings("unchecked")
@Override
public T pop() {
T item = (T) contents;
contents = contents.getNext();
count--;
return item;
}
@SuppressWarnings("unchecked")
@Override
public T peek() throws NoSuchOperationException {
T top = (T) contents;
if(top == null){
throw new NoSuchOperationException();
}
return top;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public int size() {
return count;
}
}
This is what it outputs when I call the peek method. I used my push method to add an object, and I tested it with the size method. It showed that I added an element. Then I called my pop method and displayed the size again to make sure that worked.
This is my output of the peek method:
LinearNode@33f42b49
Here is my LinearNode class:
public class LinearNode<T> {
private T element;
private LinearNode<T> next;
public LinearNode() {
this.element = null;
this.next = null;
}
public LinearNode(T element) {
this.element = element;
this.next = null;
}
public T getElement() {
return element;
}
public void setElement(T _element) {
this.element = _element;
}
public LinearNode<T> getNext() {
return next;
}
public void setNext(LinearNode<T> next) {
this.next = next;
}
}
Upvotes: 3
Views: 13853
Reputation: 103
You can simple peek by using: System.out.println("The element on top is: " + top.getElement); and return top.getElement. Remember in top you are actually removing a Node from the top of the stack and its element. Since peek only actually sees the top element of the stack you don't need to use top = top.getNext or count--;. Hope it helps!
Upvotes: 0
Reputation: 5181
pop() and peek() should return what the LinearNode references, not the actual LinkedNode.
The @SupressWarnings indicate that you are not doing generics right. Remove them, and look at what you are returning - it does not look like you are returning a `T' but the LinkedNode directly. You should not need any SupressWarnings in this code.
If you want to store Strings then you should define the stack like this:
LinkedStack<String> stack = new LinkedStack<String>();
You can think that the generic type T
will then represent a String inside your stack class.
In that case, the peek()
methods signature will then return a String.
Also: Check your pop and push methods, both should be able to do in O(1), however, you are traversing the list. You have got that backwards.
Upvotes: 1