添加和打印新节点



我正在编写一个将节点添加到列表的程序,但我似乎做错了什么…

我的java程序有三个类;Demo、Lista和Node

类节点:

public class Node {

private int num;
private Node tail;
private Node head; 
public Node (int num, Node head, Node tail) {
this.num = num;
this.head = head;
this.tail = tail;
}
}

类里:

public class Lista {

private Node nil;

public Lista () {
nil = null;
}

public void add (int num) {
Node newNode = new Node(num, head, tail);

if (head == null) {
head = newNode;
tail = newNode;
}
}

public void display () {
Node current = head;

while(current != null) {
System.out.print(current.num);
}
}
}

类演示:

public class Demo {

public static void main ( String [] args) {
Lista lista = new Lista();
lista.add(3);
lista.add(9);
lista.add(7);
lista.display();
}
}

Demo类是将不同的节点添加到列表"lista"中。类节点有num,头是下一个,尾是前一个。我怎样才能使类列表能够从类节点中使用头和尾?如果可能的话,这个代码在运行Demo时会工作吗?我应该改变/修改什么才能使其工作?

你可以这样修改你的代码:

编辑-这是一个双链表的实现。

class Node {
int num;
Node prev;
Node next;
Node(int num) {
this.num = num;
}
Node(int num, Node prev, Node next) {
this.num = num;
this.prev = prev;
this.next = next;
}
void setPrev(Node prev) {
this.prev = prev;
}
void setNext(Node next) {
this.next = next;
}
}

class Lista {
Node root;
Node endNode;
public void add(int num) {
Node n = new Node(num);
if (root == null) {
root = n;
} else {
n.setPrev(endNode);
endNode.setNext(n);
}
endNode = n;
}
public void display() {
Node iterateeNode = root;
while (iterateeNode != null) {
System.out.print(iterateeNode.num + " ");
iterateeNode = iterateeNode.next;
}
}
}

选择的答案在技术上是不正确的。对于(单)链表,Lista所需要的只是一个(头)节点。此外,Node类需要一个(next) Node字段。

下面是Node的潜在实现:

public class Node {

private Node next;
private int value;
public Node(int value) {
this.value = value;
}
public boolean hasNext() {
return next != null;
}

public Node next() {
return next;
}

public void add(Node node) {
if (next == null) {
next = node;
} else {
Node temp = next;
while (temp != null) {
temp = temp.next;
}
temp = node;
}
}
@Override
public String toString() {
return String.valueOf(value);
}
}

add()方法将在next中插入新节点,如果它为空。否则,它将遍历节点,直到找到尾节点(next为null的节点)。

Lista只有列表中的第一个元素(头节点)。

public class Lista {
private Node head;

public void add(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.hasNext()) {
temp = temp.next();
}
temp.add(node);
}
}
// Other methods
}

当调用列表中的add()函数时,它将添加新节点作为头(如果列表中没有一个),或者依赖于已经添加的节点来计算列表的末尾在哪里,以便插入新节点。

最后,要显示列表,只需覆盖节点中的toString()方法并添加"toString"值发送到字符串缓冲区,并将连接的字符串值发送到控制台,类似于下面的代码。

public void display() {
StringBuilder buff = new StringBuilder("[");
buff.append(head);
if (head != null) {
Node next = head.next();
buff.append(",");
while (next != null) {
buff.append(next);
next = next.next();
buff.append(",");
}
}
buff.append("]");
int idx = buff.lastIndexOf(",");
buff.replace(idx, idx+1, "");
System.out.println(buff.toString());
}

执行以下命令将显示[3,9,7]

public class Demo {
public static void main ( String [] args) {
Lista lista = new Lista();
lista.add(new Node(3));
lista.add(new Node(9));
lista.add(new Node(7));
lista.display();
}
}

最新更新