我正在尝试将一个元素添加到链接列表的末尾,但我不太确定如何实现此目的。以下是我正在使用的方法的当前代码:
<div id="mainDiv">
<div id="topDiv">20%</div>
<div id="bottomDiv">
<div id="gridDiv"><p>longContent</p></div>
<div id="buttonDiv">inner 20</div>
</div>
</div>
这看起来是否正确?以下是我的完整代码:
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);
}
注意:如果你想自己运行,我提供了三个我上面附带的文件。
LinearNode:
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.
EmptyCollectionException:
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;
}
}
谢谢!
答案 0 :(得分:1)
在堆栈列表中包含项目后添加数据项后,您不会重置数据结构的当前成员,因此您可能会在peek()
等方法中拥有项目。在添加第一个项目时,您似乎也没有重置current
节点。试试这个:
德尔>
答案 1 :(得分:1)
我认为这只是一个学习练习,以了解链表的内部工作原理,否则,请参阅Alex Taylor的建议。
您的方法可行,但列表增长的时间会越慢。如果您的类维护了对列表中最后一个元素的引用,则可以在执行O(1)步骤而不是在追加之前访问列表O(N)中的每个元素。
要考虑的另一件事是保护您的对象免受并发使用。如果两个线程同时在同一个对象上运行pushLast
方法,那么他们可以确定列表中与current
相同的最后一个成员,并且可以覆盖其他成员next
值,丢失数据。在Java手册中查找synchronized
。