[英]Trouble creating a LinkedStack using a generic linked-list structure in Java
我正在努力正確地實現此數據結構。 我正在使用一個堆棧接口,一個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();
}
}
在方法pop
您必須存儲top
值,然后將其設置為下一個元素,在代碼中,您總是從top返回第二個元素的值。
修改像這樣的方法pop
類的LinkedStack
應該工作:
T value = top.getData();
top = top.getNext();
size--;
return value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.