Передача элементов из массива в связанный список - PullRequest
0 голосов
/ 05 мая 2019

Я пытался создать алгоритм, который собирает некоторые элементы в связанный список, а затем передает эти элементы в массив, используя 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 голосов
/ 05 мая 2019

Полагаю, вы хотите отсортировать элементы связанного списка Было бы лучше, если бы вы отсортировали их по самому связанному списку.

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

}
...