使用Java中的通用链表结构创建LinkedStack时遇到问题

时间:2018-06-10 18:23:19

标签: java stack

我正在努力正确实现这种数据结构。我正在使用堆栈接口,LLNode类和LinkedStack类。以下类是我的代码:

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++;
    }

}

根据我的测试,push方法有效。但是,我一直在与pop相关的测试失败。我正在使用我教科书中提供给我的格式,并且几个小时以来一直在修改代码,但无济于事。我认为这是我的流行方法,但我现在还不完全确定。什么似乎是问题?

编辑:

我将在这里发布我的测试。

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();
    }


}

1 个答案:

答案 0 :(得分:0)

在方法pop中,您必须存储top的值,然后将其设置为下一个元素, 在你的代码中,你总是从顶部返回第二个元素的值。

pop的方法LinkedStack中的此类修改应该有效:

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