按升序将元素插入多链表

时间:2021-10-19 07:16:24

I have constructed a multi-linked list made up of Friend objects. The Friend class has name and age field. I am trying to insert the Friend objects in ascending order by comparing the names, but I'm running into issues with the part of my code that handles two matching names.

我构建了一个由Friend对象组成的多链表。 Friend类有名称和年龄字段。我试图通过比较名称来按升序插入Friend对象,但是我遇到了处理两个匹配名称的代码部分的问题。

I'm either getting null pointer exceptions or the printed list is out of order.

我要么得到空指针异常,要么打印列表乱序。

Populating the list

填充列表

public static void main(String[] args) {
    LinkedList l = new LinkedList();
    l.add("Travis", 19);
    l.add("Kyler", 14);
    l.add("Abby", 10);
    l.add("Bob", 19);
    l.add("Travis", 12);
    l.add("Zander", 99);
    l.printList();
}

LinkedList class:

LinkedList类:

public class LinkedList {

    Friend head;
    int listcount;

    public LinkedList(){
        head = null;
        listcount = 0;
    }

    public void add(String name, int age){
        Friend node = new Friend(name,age);
        if(head==null){
            head = node;
            return;
        }
        if(head.name.compareTo(node.name) > 0){
            node.nextName = head;
            head = node;
            return;
        }
        Friend current = head;
        Friend previous = null;

        while(current.name.compareTo(node.name) < 0 && current.nextName != null){
            previous = current;
            current = current.nextName;
        }
        if(current.name.compareTo(node.name) == 0 && current.age < node.age){
            node.nextName = current.nextName;
            current.nextName = node;
        }
        previous.nextName = node;
        node.nextName = current;
    }
    public void printList(){
        Friend temp = head;
        while(temp!=null){
            temp.print();
            temp = temp.nextName;
        }
    }
}

Friend class:

朋友类:

public class Friend {
    String name;
    int age;
    Friend nextName;
    Friend nextAge;

    public Friend(String name, int age){
        this.name = name;
        this.age = age;
        nextName = null;
        nextAge = null;
    }

    public void print(){
        System.out.println(name+" "+age+". ");
    }
}

1 个解决方案

#1


1  

Please change your add() like this(I mean the last lines of your add() method) ;

请更改你的add()(我的意思是你的add()方法的最后几行);

    if (current.name.compareTo(node.name) == 0 && current.age < node.age) {
        node.nextName = current.nextName;
        current.nextName = node;
    } else if (current.name.compareTo(node.name) < 0 ) {
        previous.nextName = current;            
        current.nextName = node;
    } else {
        previous.nextName = node;
        node.nextName = current;
    }

Output

产量

Abby 10. Bob 19. Kyler 14. Travis 12. Travis 19. Zander 99.

Abby 10. Bob 19. Kyler 14. Travis 12. Travis 19. Zander 99。

#1


1  

Please change your add() like this(I mean the last lines of your add() method) ;

请更改你的add()(我的意思是你的add()方法的最后几行);

    if (current.name.compareTo(node.name) == 0 && current.age < node.age) {
        node.nextName = current.nextName;
        current.nextName = node;
    } else if (current.name.compareTo(node.name) < 0 ) {
        previous.nextName = current;            
        current.nextName = node;
    } else {
        previous.nextName = node;
        node.nextName = current;
    }

Output

产量

Abby 10. Bob 19. Kyler 14. Travis 12. Travis 19. Zander 99.

Abby 10. Bob 19. Kyler 14. Travis 12. Travis 19. Zander 99。