Я изо всех сил пытаюсь правильно реализовать эту структуру данных.Я использую интерфейс стека, класс 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 работает.Тем не менее, я продолжаю проваливать тесты, связанные с поп-музыкой.Я использую формат, представленный мне в моем учебнике, и часами возился с кодом безрезультатно.Я думаю, что это мой поп-метод, но я не совсем уверен в этом.В чем проблема?
Редактировать:
Я собираюсь опубликовать свои тесты здесь.
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();
}
}