简体   繁体   中英

Trouble creating a LinkedStack using a generic linked-list structure in Java

I'm struggling to properly implement this data structure. I'm using a stack interface, an LLNode class, and LinkedStack class. The following classes are my code:

StackInterface.java

package stack;


public interface StackInterface<T> {

    /**
     * Removes the top most element on this stack. For convenience, the top 
    most element is returned
    * @return the top most element of this stack is returned
    * @throws StackUnderflowException if the stack is empty.
    */

    public T pop() throws StackUnderflowException;


    /**
    * Returns the top most element of this stack.
    * @return the top most element of this stack.
    * @throws StackUnderflowException if the stack is empty
    */

    public T top() throws StackUnderflowException;

    /**
    * Pushes {@code elem} to the top of this stack.
    */

    public void push(T elem);

    /**
    * Returns {@code true} if the stack contains no elements and {@code 
    false} 
    otherwise.
    * @return {@code true} if the stack contains no elements and {@code 
    false} 
    otherwise.
    */

    public boolean isEmpty();

    /**
    * Returns the number of elements in this stack.
    * @return the number of elements in this stack.
    */

    public int size();

}

LLNode.java

package stack;

public class LLNode<T> {

    private T data;
    private LLNode<T> next;  

    public LLNode(T data) {
        if (data == null)
            throw new NullPointerException();
        this.data = data;
    } 

    public T getData() {
        return data;
    }

    public void setData(T data) {
        if (data == null)
            throw new NullPointerException();
        this.data = data;
    }

    public LLNode<T> getNext() {
        return next;
    }

    public void setNext(LLNode<T> next) {
        this.next = next;
    }

}

LinkedStack.java

package stack;

public class LinkedStack<T> implements StackInterface<T> {

    protected LLNode<T> top;
    protected int size = 0;

    public T pop() throws StackUnderflowException {
        if (isEmpty()) {
            throw new StackUnderflowException("Pop on empty stack 
    attempted");
        }
        else {
            top = top.getNext();
            size--;
            return top.getData();
        }
    }

    public T top() throws StackUnderflowException {
        if (!isEmpty())
            return top.getData();
        else
            throw new StackUnderflowException("The stack is empty");
    }

    public boolean isEmpty() {
        return (top == null);
    }

    public int size() {
        return size;
    }

    public void push(T elem) {
        LLNode<T> newNode = new LLNode<T>(elem);
        top = newNode;
        newNode.setNext(top);
        size++;
    }

}

According to my tests, the push method works. However, I keep failing tests related to pop. I'm using the format presented to me in my textbook and have been tinkering with the code for hours now to no avail. I think it's my pop method, but I'm not entirely sure at this point. What seems to be the problem?

Edit:

I'm going to post my tests here.

package stack;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

public class PublicLinkedStackTest {

    private StackInterface<Integer> stack;

    @Before
    public void setup(){
        stack = new LinkedStack<Integer>();
    }

    @Test (timeout = 500)
    public void testStack() {
        assertTrue("Stack should be empty after being constructed.", 
        stack.isEmpty());
        assertEquals("Stack should contain one element after being 
        constructed.", 0, stack.size());

        stack.push(5);
        assertFalse("Stack should not be empty.", stack.isEmpty());
        assertEquals("The top element should be 5", new Integer(5), 
        stack.top());
        assertEquals("The stack should contain one element.", 1, 
        stack.size());

        stack.push(4);
        assertEquals("The top element should be 4", new Integer(4), 
        stack.top());
        assertEquals("The stack should contain two elements.", 2, 
        stack.size());

        Integer t = stack.pop();
        assertEquals("The popped element should be 4", new Integer(4), t);
        assertEquals("The top element should be 5", new Integer(5), 
        stack.top());
        assertEquals("The stack should contain one element.", 1, 
        stack.size());
        assertFalse("The stack should not be empty.", stack.isEmpty());

        t = stack.pop();
        assertEquals("The popped element should be 5", new Integer(5), t);
        assertTrue("The stack should be empty.", stack.isEmpty());
    }

    @Test (timeout = 500, expected = StackUnderflowException.class)
    public void testStackUnderflowPop(){
        stack.pop();
    }

    @Test (timeout = 500, expected = StackUnderflowException.class)
    public void testStackUnderflowPop2(){
        stack.push(5);
        stack.pop();
        stack.pop();
    }

    @Test (timeout = 500, expected = StackUnderflowException.class)
    public void testStackUnderflowPop3(){
        stack.push(5);
        stack.push(6);
        stack.pop();
        stack.pop();
        stack.pop();
    }

    @Test (timeout = 500, expected = StackUnderflowException.class)
    public void testStackUnderflowTop(){
        stack.top();
    }

    @Test (timeout = 500, expected = StackUnderflowException.class)
    public void testStackUnderflowTop2(){
        stack.push(5);
        stack.pop();
        stack.top();
    }

    @Test (timeout = 500, expected = StackUnderflowException.class)
    public void testStackUnderflowTop3(){
        stack.push(5);
        stack.push(6);
        stack.pop();
        stack.pop();
        stack.top();
    }


}

in method pop you have to store value of top and then set it to next element, in your code you are always returning value of second element from top.

Modification like this in method pop of class LinkedStack should work:

T value = top.getData();
top = top.getNext();
size--;
return value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM