繁体   English   中英

如何将 addElement 方法写入已排序的 LinkedList?

[英]How do I write an addElement method to a sorted LinkedList?

我有一个任务:

使用类 Node 实现 String 对象的链接列表(请参阅 Big > Java Early Objects 16.1.1)。 编写方法,可以插入 > 和删除对象,以及打印列表中的所有对象。 >要求列表中的所有元素始终根据字符串(Comparable)的自然顺序进行排序。

我似乎无法理解的方法是 addElement 方法

整个班级在这里: https ://pastebin.com/Swwn8ykZ 和 mainApp: https ://pastebin.com/A22MFDQk

我已经阅读了这本书(Big Java Early Objects),以及 geeksforgeeks

public void addElement(String e) {
    Node newNode = new Node();
    if (first.data == null) {
        first.data = e;
        System.out.println("Success! " + e + " has been 
added!");
    } else if (first.data.compareTo(e) == 0) {
        System.out.println("The element already exists in the 
list");
    } else {
        while (first.next != null) {
            if (first.next.data.compareTo(e) != 0) {
                first.next.data = e;
            } else {
                first.next = first.next.next;
            }
        }
    }
}

public static void main(String[] args) {
    SortedLinkedList list = new SortedLinkedList();

    String e1 = new String("albert");
    String e2 = new String("david");
    String e3 = new String("george");
    String e4 = new String("jannick");
    String e5 = new String("michael");

    // ----------------SINGLE LIST--------------------------
    list.addElement(e1);
    list.addElement(e2);
    list.addElement(e3);
    list.addElement(e4);
    list.addElement(e5);

    System.out.println("Should print elements after this:");
    list.udskrivElements();
 }
}

预期结果:列表中打印的五个名称

实际结果:打印的名字

给定此Node类:

private class Node {
    public String data;
    public Node next;
}

private Node first;private Node first;的类级别字段private Node first; 最初为null表示空列表,则addElement可能像这样:

public void addElement(String text) {
    if (text == null) return; // don't store null values

    Node extra = new Node();
    extra.data = text;

    if (first == null) {
        // no list yet, so create first element
        first = extra;
    } else {
        Node prev = null; // the "previous" node
        Node curr = first; // the "current" node

        while (curr != null && curr.data.compareTo(text) < 0) {
            prev = curr;
            curr = curr.next;
        }

        if (curr == null) {
            // went past end of list, so append
            prev.next = extra;
        } else if (curr.data.compareTo(text) == 0) {
            System.out.println("Already have a " + text);
        } else {
            // between prev and curr, or before the start
            extra.next = curr;
            if (prev != null) {
                prev.next = extra;
            } else {
                // append before start, so 'first' changes
                first = extra;
            }
        }
    }
}

顺便说一句,还要尝试按未排序的顺序添加名称,以检查列表是否对它们进行了排序(尝试时在代码中发现了一个错误)。

像这样定义一个Node

class Node 
{ 
       int data; 
       Node next; 
} 

下面的方法将元素插入到排序顺序:

void sortInsert(Node new_node){
    Node current; 
    if (head == null || head.data >= new_node.data) 
    { 
        new_node.next = head; 
        head = new_node; 
    } 
    else { 
           current = head; 
           while ( current.next.data < new_node.data && current.next != null)
            current = current.next; 
     new_node.next=current.next;
     current.next=new_node;

    } 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM