插入和删除链表错误

时间:2017-07-05 23:23:11

标签: java class linked-list

手动编写LinkedList类。一直试图解决这个问题几天。我需要能够从列表中插入和删除节点。插入和删除似乎工作正常,直到列表中有四个或更多节点。在链接了几个节点之后,值开始插入列表的末尾,然后从那里搞乱所有插入。对于删除,删除有时会“删除”第一个节点,使第二个节点成为新头。我看不出错误在哪里。我有main实现插入/删除方法。代码下面是示例输出,但这是我的代码:

class SortedLinkedList {
private int size;
private Node head;

public SortedLinkedList() {
    size = 0;
    head = null;
} //constructor

public Node appropriatePosition(int k) {
    Node curr = head;

    if(head == null)
        return head;
    else if(curr.data >= k)
        return null;
    else if(curr.next == null)
        return curr;
    else if(curr.next.data > k)
        return curr;
    while(curr.next != null) { //null? write new if
        curr = curr.next; 
        }
    return curr;
}

public void insert(int k) {
    Node appPos = appropriatePosition(k);
    Node insNode = new Node(k);

    if(appPos == null && head != null) {
        insNode.next = head;
        head = insNode;
    }    
    else if(appPos == null && head == null)
        head = insNode;
    else {
        insNode.next = appPos.next;
        appPos.next = insNode;
    }
    size++;
}

public Node find(int p) {
    if(head == null)
        return null;
    if(head.data == p)
        return head;

    Node curr = head;
    Node prev = null;
    while(curr.data != p) {
        prev = curr;
        curr = curr.next;
        if(curr == null)
            return null;
        else if(curr.data > p) {
            System.out.println("Value not found");
            return curr;
        }
        else if(curr == null)
            return null;
    }
    return prev;        
}

public void delete(int q) {
    Node findVal = find(q);
    if(head == null)
        System.out.println("Cannot delete: SortedLinkedList is empty");
    else if(findVal.data == head.data)
        head = head.next;
    else if(head.next == null)
        System.out.println("Cannot find value");
    else if(findVal == null)
            System.out.println("Cannot delete: SortedLinkedList is empty");
    else if(findVal.data > q)
        System.out.println("Value not found");
    else  {
        findVal.next = findVal.next.next;
        size--;
    }    
}

class Node {
public Node next = null;
public int data;   

public Node(int k){
    data = k;
}
public Node(Node s) {
    data = s.data;
    next = s;
}        
}

插入的示例输出:

插入 输入值:2打印: 2 插入 输入值:5打印: 2 5 插入 输入值:5打印: 2 5 5 插入 输入值:7打印: 2 5 5 7 插入 输入值:-9打印: -9 2 5 5 7 插入 输入值:-3打印: -9 -3 2 5 5 7 插入 输入值:6打印: -9 -3 2 5 5 7 6 插入 输入值:4打印: -9 -3 2 5 5 7 6 4 插入 输入值:6 -9 -3 2 5 5 7 6 4 6

删除示例输出:

删除 输入值:7打印: -9 -3 2 5 5 6 4 6 删除 输入值:2打印: -9 -3 5 5 6 4 6 删除 输入值:-3打印: -3 5 5 6 4 6 删除 输入值:-3打印: 5 5 6 4 6

1 个答案:

答案 0 :(得分:0)

你的删除是在检查错误的内容。这种删除更简单,并且可以解决问题

public void deleteNew(int p) {
        Node curr = head;
        Node prev = null;
        while (curr != null) {
            if (curr.data == p) {
                prev.next = curr.next;
                size--;
                break;
            }
            prev = curr;
            curr = curr.next;
        }
    }

接下来我将处理插入。

public void insertNew(int k){
    Node prev = null, curr = head;

    Node insert = null;
    if (head == null){
        head = new Node(k);
        size++;
    }
    else{
        while (curr != null) {
            if (curr.data > k) {
                insert = new Node(k);
                insert.next = curr;
                if (prev != null)
                    prev.next = insert;
                size++;
                if (insert.data < head.data)
                    head = insert;
                break;
            }
            prev = curr;
            curr = curr.next;
        }
    }
}

保存重大遗漏这些应该更简单,并做你需要他们做的事情。试过

SortedLinkedList l = new SortedLinkedList();
        l.insertNew(5);
        l.insertNew(4);
        l.insertNew(3);
        l.insertNew(2);
        l.insertNew(1);
        l.insertNew(0);
        l.deleteNew(5);
        l.deleteNew(1);
        l.insertNew(1);