gpt4 book ai didi

java - 实现一个 toString 方法来打印出一个 LinkedList

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:04:58 25 4
gpt4 key购买 nike

我的 OOP 类(class)项目遇到了问题。我快完成了,但仍然缺少 toString 方法和 main 方法。不太确定如何去做,希望得到任何帮助。我想要我的 toString方法的作用如下:

Returns a string representation of all the items stored in the list. A string representation of an empty list looks like head--><--tail A string representation of a non-empty list looks like: head-->134<-->-8<-->42<-->1<--tail

public class IntegerNode{

private IntegerNode next;
private IntegerNode prev;
private int data;

public IntegerNode(int data){
next = next;
prev = prev;
data = data;
}

public int getData(){
data = data;
return this.data;
}

public IntegerNode getNext(){
return next;
}

public IntegerNode getPrevious(){
return prev;
}

public void setNext(IntegerNode in){
prev = in;
}

public void setPrevious(IntegerNode in){
prev = in;
}

}

这是我目前在 IntegerLinkedList 类中的内容

public class IntegerLinkedList{

private IntegerNode head;
private IntegerNode tail;

public IntegerLinkedList(){
head = null;
tail = null;
}

public void addFirst(int x){
IntegerNode nH = new IntegerNode(x);
if (head == null) {
head = nH;
tail = nH;
}else{
head.setPrevious(nH);
nH.setNext(head);
head = nH;

}
}

public void addLast(int x){
IntegerNode t = new IntegerNode(x);
if (tail == null){
head = t;
tail = t;
}else{
tail.setNext(t);
t.setPrevious(tail);
tail = t;
}
}

public int peekFirst(){
return head.getData();
}

public int peekLast(){
return tail.getData();
}

public String toString(){
if (head == null && tail == null){
String empty = "head--><--tail";
return empty;
}else{
String h = "Head--> " + head;
String t = tail + " <--Tail";
String m = " <--> ";
// while(IntegerNode.getNext() != null)
//}
//return h + m + t;

}
}

public int pollFirst(){
int x = head.getData();
head = head.getNext();
head.setPrevious(null);
return x;
}

public int pollLast(){
int x = tail.getData();
tail = tail.getPrevious();
tail.setNext(null);
return x;
}

}

我在想 while 循环是到达此处的方式,但话又说回来我不确定。

最佳答案

写法如下:

@Override  // <-- Annotate that you are overriding the toString() method
public String toString(){
if (head == null && tail == null){
String empty = "head--><--tail";
return empty;
}else{
StringBuilder sb = new StringBuilder();
sb.append("Head-->");

IntegerNode curr = head;
sb.append(curr.getData());
curr = curr.getNext();
while(curr != null) {
sb.append("<-->");
sb.append(curr.getData());
curr = curr.getNext();
}
sb.append("<--tail");
return sb.toString();
}
}

作为替代方案,您可以简化逻辑以不使用外部 if else:

@Override  // <-- Annotate that you are overriding the toString() method
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Head-->");

IntegerNode curr = head;

if (curr == null)
{
sb.append("<--tail");
return sb.toString();
}

sb.append(curr.getData());
curr = curr.getNext();
while(curr != null) {
sb.append("<-->");
sb.append(curr.getData());
curr = curr.getNext();
}
sb.append("<--tail");

return sb.toString();
}

关于java - 实现一个 toString 方法来打印出一个 LinkedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37766590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com