简体   繁体   中英

Deleting a node from a linked list in Java

I'm trying to write a code that handles a linked list. This linked list is somehow different because it's a key-value pair linked list (singly). The linked list should provide the user with basic functions, such as retrieving the size of linked list, checking for a key whether it's in the list or not, insertion, and deletion. It seems that all the functions are working properly except for the deletion. The code for the deletion method run correctly with no run time error, but it gives me results that's not what I wanted to have. Here is my code:

public class SequentialSearch<Key,Value> {

    private int N;     // number of key-value pairs
    private Node head; // the linked list of key-value pairs
    private Node tail;

    // a helper linked list data type
    private class Node {
        private Key key;
        private Value val;
        private Node next;

        public Node(Key key, Value val, Node next) {
            this.key = key;
            this.val = val;
            this.next = next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    public SequentialSearch() {
    }

    public int size() {
        if (head == null)
            return 0;
        else {
            Node x = head;
            while (x.next != null) {
                N++;
                x = x.next;
            }
        }
        return N;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public boolean contains(Key key) {
        return get(key) != null;
    }

    public Value get(Key key) {
        for (Node x = head; x != null; x = x.next) {
            if (key.equals(x.key))
                return x.val;
        }
        return null;
    }

    public void put(Key key, Value val) {
        if (val == null) {
            delete(key);
            return;
        }

        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key)) {
                x.val = val;
                return;
            }
        }
        first = new Node(key, val, first);
        N++;
    }

    public boolean delete(Key key) {
        Node curr = head;
        Node prev = null;
        boolean result = false;

        if(isEmpty())
            System.err.println("Error: The list is empty.");
        while(curr != null) {
            if(key.equals(curr.key)) {
                if(curr.equals(head)) {
                    head = curr = null;
                    N--;
                    result = true;
                    return result;
                } else {
                    prev.next = curr.next;
                    curr.setNext(null);
                    N--;
                    result = true;
                    return result;
                }
            } else {
                prev = curr;
                curr = curr.next;
            }
        }
        return result;
    }
}

I've written a main program to test for the add (put) and deletion, but it seems to be working for the insertion but not for the deletion. I think I might have a problem the deletion in case there is only one node in the list and the case of deleting a node from the middle of the list.

I'm also trying to modify the deletion method by writing a new method using the recursion, but I faced some errors -also logically. Here is the code of that function:

public void delete(Key key) {
    first = delete(first, key);
}

private Node delete(Node x, Key key) {
    if (x == null) return null;
    if (key.equals(x.key)) {
        N--;
        return x.next;
    }
    x.next = delete(x.next, key);
    return x;
}

Could you please tell me what did I do wrong?

head = curr = null;

This statement is a little confusing but I think it might be your problem for when deleting. If you're deleting a match that is at 'head', you just want to set head to 'curr.next'

What I think is going on: delete(a)

a -> b -> c -> d

head = a
curr = a
curr.next = b

you set head to null, but head needs to point to the first item in the new list, which if you deleted 'a', would be 'b', or curr.next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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