[英]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.
我正在使用一个堆栈接口,一个LLNode类和LinkedStack类。 The following classes are my code:
以下类是我的代码:
StackInterface.java 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 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 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. 根据我的测试,push方法有效。 However, I keep failing tests related to pop.
但是,我一直未能通过与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. 在方法
pop
您必须存储top
值,然后将其设置为下一个元素,在代码中,您总是从top返回第二个元素的值。
Modification like this in method pop
of class LinkedStack
should work: 修改像这样的方法
pop
类的LinkedStack
应该工作:
T value = top.getData();
top = top.getNext();
size--;
return value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.