我正在尝试将一个元素添加到链接列表的末尾,但我不太确定如何完成此操作。这是我正在使用的方法的当前代码:
public void pushLast(T element) {
LinearNode<T> temp = new LinearNode<T>(element);
if (isEmpty()) {
top = temp;
} else {
LinearNode<T> current = top;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(temp);
}
这看起来正确吗?这是我使用此方法的完整代码:
import java.lang.StringBuilder;
public class Murray_A05Q3 {
public static void main(String[] args) {
LinkedStack<Integer> stack = new LinkedStack<Integer>();
System.out.println("STACK TESTING");
System.out.println("The stack contains:\n" + stack.toString());
stack.push(3);
stack.push(7);
stack.push(4);
System.out.println(stack.peek());
stack.pop();
stack.push(9);
stack.push(8);
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.peek());
System.out.println("The size of the stack is: " + stack.size());
System.out.println("The stack contains:\n" + stack.toString());
} // End of method header.
public static class LinkedStack<T> implements StackADT<T> {
private int count;
private LinearNode<T> top; // serves as node class
// Creating an empty stack
public LinkedStack() {
count = 0;
top = null;
int pushLast;
}
@Override
public void push(T element) {
LinearNode<T> temp = new LinearNode<T>(element);
temp.setNext(top);
top = temp;
count++;
}
public T pop() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("stack");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
public void pushLast(T element) {
LinearNode<T> temp = new LinearNode<T>(element);
if (isEmpty()) {
top = temp;
} else {
LinearNode<T> current = top;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(temp);
}
}
public T peek() throws EmptyCollectionException {
return top.getElement();
}
public boolean isEmpty() {
return (top == null);
}
public int size() {
return count;
}
public String toString() {
if (isEmpty()) {
return " ";
}
StringBuilder sb = new StringBuilder(top.toString());
LinearNode<T> next = top.getNext();
while(next != null) {
sb.append("\n").append(next.getElement());
next = next.getNext();
}
return sb.toString();
} // End of the toString method.
} // End of method header.
} // End of class header.
注意:以防万一您想自己运行它,我提供了附加到上面文件的三个文件。
线性节点:
public class LinearNode<T> {
private LinearNode<T> next;
private T element;
public LinearNode() {
next = null;
element = null;
}
public LinearNode(T elem) {
next = null;
element = elem;
}
public LinearNode<T> getNext() {
return next;
}
public void setNext(LinearNode<T> node) {
next = node;
}
public T getElement() {
return element;
}
public void setElement(T elem) {
element = elem;
}
}
空集合异常:
public class EmptyCollectionException extends RuntimeException {
public EmptyCollectionException(String collection) {
super("The " + collection + " is empty.");
}
}
谢谢!
最佳答案
添加数据项后,当堆栈列表中有项时,您不会重置数据结构的当前成员,因此您可能会在 peek()
.添加第一个项目时,您似乎也没有重置 current
节点。尝试这个:
编辑:我想我是在太累的时候回答这个问题的,而且是不正确的。
关于java - 如何将元素添加到链表末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778395/