如何在Java中的排序列表中插入元素

时间:2014-05-09 13:53:22

标签: java list class sorting insert

我必须在java中创建自己的列表。必须对要插入的元素(Person元素)进行排序(从低到高)。 所以我创建了一个方法compareTo来检查插入的Person的名称。 问题在于插入方法。我写了这段代码:

public boolean insert(Person p){
    PersonListElement element = new PersonListElement(p);
    boolean inserted = false;
    if(containsElement(p) == true){
        inserted = false;
        return inserted;
    }
    if(head == null){
        head = element;
        size = size + 1;
        inserted = true;
        return inserted;
    }
    PersonListElement position = head;
    PersonListElement prev = null;
    while(position != null){
        if(position.getValue().compareTo(element.getValue()) < 0){
            if(prev != null){
                prev.setNext(element);
                element.setNext(position);
                inserted = true;
                position = null;
            }
            prev = position;
            position = position.getNext(); //here i get pointerException prob.
        }
    }
    if(inserted == false && prev != null){
        prev.setNext(element);
        inserted = true;
    }
    size = size + 1;
    return inserted;
}

这是我的方法,但尚未完成。你能救我吗?

编辑:

我也试图制作删除元素的方法。我认为我的代码适用于空列表,1个大小列表,如果元素不在列表的末尾。但我不知道我的方法是否删除了最后一个元素:

public boolean delete(Person p){
    boolean deleted = false;
    if(size == 0){
        deleted = false;
        return deleted;
    }
    if(size == 1 && head.getValue().compareTo(p) == 0){
        head = null;
        deleted = true;
        return deleted;
    }
    PersonListElement current = head;
    PersonListElement prev = null;
    PersonListElement next = head;
    next = next.getNext();
    while(next != null){
        if(current.getValue().compareTo(p) == 0){
            prev.setNext(next);
            deleted = true;
            return deleted;
        }
        prev = current;
        current.getNext();
        next.getNext();
    }
    return deleted;
}

用这个课我测试了我的代码:

class SortList
{
    public static void main(String args[]){
        SortedList list = new SortedList();
        Person a = new Person("Alice", 10);
        list.insert(a);
        a = new Person("Al", 20);
        list.insert(a);
        a = new Person("Bo", 90);
        list.insert(a);
        p = new Person("Cin", 3);
        list.insert(a);     
        System.out.println(list);
        }
}

1 个答案:

答案 0 :(得分:1)

您需要完成迭代以及插入。假设您正在实施链接列表,并且您在next课程中拥有公开ListElement属性。

public boolean insert(Person p){
        ListElement element = new StackElement(p);
        boolean inserted = false;
        if(head == null){
            head = element;
            size = size + 1;
            inserted = true;
            return inserted;
        }
        ListElement temp = head;
        ListElement prev = null;
        while(temp != null){
            if(temp.compareTo(element) < 0) {
                    if(prev != null) prev.next = element; //Keep the chain connected
                    element.next = temp; //Insert you element before temp
                    inserted = true;
                    break; 
                }
                prev = temp;
                temp = temp.next;
            }
        }
        if(!inserted && temp == null && prev != null) { //You may want to insert it at the end of the list if no upper element is found.
                 prev.next = element; //Put it at the end.
                 inserted = true;
        }
       if(inserted) size = size + 1;
       return inserted; //This should be true in any case but I left it for the case you want to avoid inserting the element at the end if its place is not found.
    }

ListElement.next也可以是私有或受保护的属性,可以通过getNext()之类的getter进行访问。

我认为你不应该返回inserted值,因为如果元素不能放在任何其他元素之前,它应该放在列表的末尾,因此总是被插入。也就是说,此代码是排序列表的更好实现:

public void insert(Person p){
        ListElement element = new StackElement(p);

        if(head == null){
            head = element;
            size = size + 1;
            return;
        }

        ListElement temp = head;
        ListElement prev = null;

        boolean inserted = false;
        while(temp != null){
            if(temp.compareTo(element) < 0) {
                    if(prev != null) prev.next = element; //Keep the chain connected
                    element.next = temp; //Insert you element before temp
                    inserted = true;
                    temp = null;
                } else {
                    prev = temp;
                    temp = temp.next;
                }
            }
        }
        if(!inserted && prev != null)
        { 
                 prev.next = element; //Put it at the end.
        }
       size = size + 1;
    }

关于你的delete方法,我认为你的事情太复杂了。 在迭代中,您只需要保留两个引用:一个用于前一个元素,另一个用于当前元素。 如果您的第一个元素是要删除的元素,则prev引用将为null,您必须更新head。否则,您需要更新prev下一个引用,使其引用current之后的元素:

public boolean delete(Person p){
    boolean deleted = false;
    PersonListElement current = head;
    PersonListElement prev = null;
    while(current != null && !deleted){
        if(current.getValue().compareTo(p) == 0){
            if(prev != null) 
                prev.setNext(current.getNext());
            else
                head = current.getNext();
            deleted = true;
        } else {
        prev = current;
        current = current.getNext();
       }
    }
    if(deleted) size--;
    return deleted;
}