【问题标题】:How can I add an item to the end the a Linked List?如何将项目添加到链接列表的末尾?
【发布时间】:2021-10-19 01:37:20
【问题描述】:

我正在为我的数据结构类开发一个项目,该项目要求我编写一个类来实现整数的链表。

  • 为节点使用内部类。
  • 包括以下方法。
  • 编写一个测试器,使您能够以任何顺序使用您想要的任何数据来测试所有方法。

我必须创建一个名为“public void addToBack(int item)”的方法。此方法旨在“将项目添加到列表末尾”我在下面有此方法的代码。当我执行此方法时,我的列表变为空。有人知道我做错了什么以及如何解决吗?


import java.util.Random;
import java.util.Scanner;

public class LinkedListOfInts {
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

        public Node(int value, Node nextNode) {
            this.value = value;
            this.nextNode = nextNode;
        }

    }

    public LinkedListOfInts(LinkedListOfInts other) {
        Node tail = null;
        for (Node n = other.head; n != null; n = n.nextNode) {
            if (tail == null)
                this.head = tail = new Node(n.value, null);
            else {
                tail.nextNode = new Node(n.value, null);
                tail = tail.nextNode;
            }
        }
    }

    public LinkedListOfInts(int[] other) {
        Node[] nodes = new Node[other.length];
        for (int index = 0; index < other.length; index++) {
            nodes[index] = new Node(other[index], null);
            if (index > 0) {
                nodes[index - 1].nextNode = nodes[index];
            }
        }

        head = nodes[0];
    }

    public LinkedListOfInts(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i++)
            this.addToFront(random.nextInt(high - low) + low);
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    public void addToBack(int x) {
        if (head == null) {
            head = new Node(x, head);
            return;
        }
        tail = head;
        while (tail.nextNode != null) {
            tail = tail.nextNode;
        }
        tail.nextNode = new Node(x, tail);
    }

    public String toString() {
        String result = "";
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
            if (!result.isEmpty()) {
                result += ", ";
            }
            result += ptr.value;
        }
        return "[" + result + "]";
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        boolean done = false;
        while (!done) {
            System.out.println("1. Add to Back");
            System.out.println("2. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("Add an Item to the Back of a List.");
                list.addToBack(input.nextInt());
                break;
            case 2:
                System.out.println("toString");
                System.out.println(list.toString());
                break;

            }
        }
    }
}

【问题讨论】:

    标签: java list methods linked-list nodes


    【解决方案1】:

    当你添加到tail 时,nextNode 应该指向 null

    tail.nextNode = new Node(x, null);
    

    此时你有一个无限循环

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 2021-12-23
      相关资源
      最近更新 更多