【问题标题】:Passing elements from an array to a linked list将元素从数组传递到链表
【发布时间】:2019-05-05 09:31:40
【问题描述】:

我试图创建一种算法,将一些元素放入链接列表,然后使用冒泡排序将这些元素传递到数组中以对元素进行排序。我对这些元素进行了排序,但现在我无法再次将数组中的元素传递到链接列表。

我使用passIntoArray(Node head, int arr[]) 方法从链接列表中获取元素并传递到数组中。方法count(Node head) 用于检查链接列表中有多少元素(当前用作数组的索引)。谁能帮帮我?

package LinkedList; 

public class LinkedListSorting {

    private static int count(Node head) {
        if (head == null) return 0;
        Node current = head;
        int count = 0;

        while(current != null) {
            count++;
            current = current.next;
        } return count;
    }

    private static void passIntoArray(Node head, int arr[]) {
        if (head == null) return;
        Node current = head;
        int i = 0;

        while(current != null) {
            arr[i] = current.data;
            i++;
            current = current.next;
        } 

        int length = count(head);

        for(i=0; i<length; i++) {
            for(int j=i+1; j<length; j++) {
                if(arr[i]>arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        for(i=0; i<length; i++) {
            System.out.println(arr[i]);
        }
    }


    //FROM THE ARRAY TO THE LL



    private static void display(Node head) {
        if (head == null) return;
        Node current = head;

        while(current != null) {
            System.out.print(current.data + " --> ");
            current = current.next;
        } System.out.println(current);
    }

    private static class Node{
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }


    }

    public static void main(String args[]) {
        Node head = new Node(5);
        Node first = new Node(2);
        Node second = new Node(3);
        Node third = new Node(4);
        Node fourth = new Node(1);

        head.next = first;
        first.next = second;
        second.next = third;
        third.next = fourth;

        display(head);

        System.out.println();

        int arr[] = new int[count(head)];

        passIntoArray(head, arr);

        System.out.println();
    }

}

【问题讨论】:

  • 为什么不清除列表并将数组中的项目添加到空列表中?
  • 实际上,当我将链表中的元素传递到数组中时,我试图打印出 LL,但它是空的。我想将数组元素传递到 LL

标签: java arrays algorithm linked-list


【解决方案1】:

我猜你想对链表的元素进行排序 如果你在链表本身中对它们进行排序会更好。

public class Main {

    private static int count(Node head) {
        if (head == null) return 0;
        Node current = head;
        int count = 0;

        while(current != null) {
            count++;
            current = current.next;
        } return count;
    }

    private static void sortLL(Node head) {
        if (head == null) return;
        Node current = head;
        Node i,j;

        int length = count(head);

        for(i=head; i!=null; i=i.next) {
            for(j=i.next; j!=null; j=j.next) {
                if(i.data>j.data) {
                    int temp = i.data;
                    i.data = j.data;
                    j.data = temp;
                }
            }
        }

    }

    private static void display(Node head) {
        if (head == null) return;
        Node current = head;

        while(current != null) {
            System.out.print(current.data + " --> ");
            current = current.next;
        } System.out.println(current);
    }

    private static class Node{
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }


    }

    public static void main(String args[]) {
        Node head = new Node(5);
        Node first = new Node(2);
        Node second = new Node(3);
        Node third = new Node(4);
        Node fourth = new Node(1);

        head.next = first;
        first.next = second;
        second.next = third;
        third.next = fourth;

        sortLL(head);
        display(head);
    }

}

【讨论】:

  • 您好,非常感谢您的回答!我是初学者,所以我不知道如何直接在链表中对元素进行排序。非常感谢:D
  • 我认为count方法现在没用了,因为我用它作为数组的索引
  • 我很高兴随时询问更多信息
猜你喜欢
  • 2020-12-06
  • 1970-01-01
  • 2014-09-17
  • 2019-05-30
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多