将元素从数组传递到链接列表

时间:2019-05-05 09:31:40

标签: java arrays algorithm linked-list

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

我使用方法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();
    }

}

1 个答案:

答案 0 :(得分:0)

我猜您想对链接列表的元素进行排序 如果将它们放在链接列表中进行排序,那会更好。

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);
    }

}