将数组转换为链接列表

时间:2013-10-23 03:35:21

标签: java singly-linked-list

我有一个字符数组,我正在尝试将每个字符转换为一个链接到下一个节点的节点。问题是我一直陷入无限循环中,我不明白为什么。这是我的代码:

String map = "ABBACBCCA";
char[] charArray = map.toCharArray();
ListNode head;
ListNode temp;
ListNode next;

for (int i = 0; i < charArray.length - 1; i++) {
     temp = new ListNode(charArray[i]);
     next = new ListNode(charArray[i+1]);
     temp.next = next;

     if (i == 0) {
          head = temp;
     }
}

ListNode类看起来像:

class ListNode<T> {
     public T data = null;
     public ListNode next = null;

     public ListNode(T data) {
          this.data = data;
     }
}

看起来它会进入for循环的最后一次迭代,然后陷入无限循环..任何人都知道为什么?

4 个答案:

答案 0 :(得分:1)

对于开始,我认为你会想要:

next = new ListNode(charArray[i]);

next = new ListNode(charArray[i+1]);

我注意到的其他事情:

for (int i = 0; i < charArray.length - 1; i++) {
     temp = new ListNode(charArray[i]);
     next = new ListNode(charArray[i+1]);
     temp.next = next;

          if (i == 0) {
            head = temp;
           }
     }

我认为这不会产生你想要的东西。它不会给你A-> B-> B-> A等等。它会给出更多 - &gt; A-> B,B-> B等等。不确定这就是你所追求的。

我认为这应该会变得更好:

String map = "ABBACBCCA";
        ListNode<Character> head = null;
        ListNode<Character> newHead = null;
        ListNode<Character> next = null;

        char[] charArray = map.toCharArray();

        head = newHead = new ListNode<Character>(charArray[0]);
        for (int i = 1; i < charArray.length - 1; i++) {
            next = new ListNode<Character>(charArray[i]);

            newHead.next = next;

            newHead = next;
        }

基本上创建和链接,创建和链接。 (测试对我来说很好)传入丑陋!

System.out.println(head.data);
        ListNode<Character> nextptr = head.next;
        while (true) {

            if (nextptr.next == null) {
                break;
            }
            System.out.println(nextptr.data);
            nextptr = nextptr.next;
        }

答案 1 :(得分:0)

如果您想继续开发自己的代码,最好使用调试器。你应该创建一些公共方法来设置LinkList节点的下一个元素,就像我在这个例子中所做的那样。解释会很冗长,所以这是代码。

public class Test {

 public static void main(String[] args) {
    ListNode<Character> head = new Test().arrayToLinkList();

    while ((head = head.nextNode()) != null) {
        System.out.println(head.readData());
    }
 }

 public ListNode<Character> arrayToLinkList() {
    String map = "ABBACBCCA";
    char[] charArray = map.toCharArray();
    ListNode<Character> head, next;
    head = new ListNode<Character>(charArray[0]);
    next = head;
    for (int i = 0; i < charArray.length - 1; i++) {
        next = next.next(new ListNode<Character>(charArray[i + 1]));
    }
    return head;
 }
}

class ListNode<T> {
 private T data = null;
 private ListNode<T> next = null;

 public ListNode(T data) {
    this.data = data;
 }

 public ListNode<T> next(ListNode<T> next) {
    this.next = next;
    return this.next;
 }

 public ListNode<T> nextNode() {
    return this.next;
 }

 public T readData() {
    return data;
 }
}

答案 2 :(得分:0)

您的参考变量temp&amp; next在每次迭代期间分配给新对象,并且您将忽略下一个指针。你可以像其他人建议的那样使用调试器来解决这个问题。这是工作示例。

public class Test {

    public static void main(String args[]) {
        String map = "ABBACBCCA";
        char[] charArray = map.toCharArray();
        ListNode<Character> head = null, temp = null;
        for (int i = 0; i < charArray.length; i++) {
            ListNode<Character> obj = new ListNode<Character>(charArray[i]);
            if (temp != null) {
                temp.next = obj;
            } else {
                head = obj;
            }
            temp = obj;
        }
        // Print the list
        while (head != null) {
             System.out.println(head.data);
             head = head.next;
        }
    }
}

class ListNode<T> {
    public T data = null;
    public ListNode<T> next;
    public ListNode(T data) {
        this.data = data;
        this.next = null;
    }
}

答案 3 :(得分:0)

这可能是构建链表的简单方法之一:

String map = "ABBACBCCA";

ListNode<Character> head = null;
ListNode<Character> tail = null;

for (char c:map.toCharArray()) {
  final ListNode<Character> node = new ListNode<>(c);
  if (head == null) {
    head = node;
  } else {
    tail.next = node;
  }
  tail = node;
}

在各处处理你的类型参数

class ListNode<T> {
  public T data = null;
  ListNode<T> next = null;

  public ListNode(T data) {
    this.data = data;
  }
}

更不用说您也可以充分利用Java api's

LinkedList<Character> ll =
  "ABBACBCCA".chars()
    .mapToObj(i->(char)i) // takes care of boxing char
    .collect(Collectors.toCollection(LinkedList<Character>::new));

要循环浏览ListNode,您可以考虑添加:

class ListNodeIterator<T> implements Iterator<T> {
  private ListNode<T> current;

  public ListNodeIterator<T> ListNodeIterator(ListNode<T> node) {
    current = node;
  }

  public boolean hasNext() {
    return current.next != null;
  }

  public T next() {
    current = current.next;
    return current.data;
  }
}

进行以下修改:

class ListNode<T> implements Iterable<T> {
  public T data = null;
  public ListNode<T> next = null;

  public ListNode(T data) {
    this.data = data;
  }

  public Iterator<T> iterator() {
    return new ListNodeIterator<>(this);
  }
}

所以你可以按如下方式使用它:

for (char c:head) {
  System.out.println("Character: "+c);
}

甚至

head.forEach(c->{System.out.println("Character: "+c);});

嗯......这不是一次通过Javaland的好旅行吗?