简体   繁体   中英

Implementing isEmpty() for Linked Dictionary from scratch in Java

Currently, I'm working on a project in which I am implementing a Linked Dictionary interface from scratch. It was smooth sailing until I realized my code claims the entire dictionary is empty as soon as it sees a single null value.

public class LinkedDictionary<K, V> implements Dictionary<K, V> {

    /** List node. */
    private class Node {
        K key;
        V value;
        Node next;

        Node(K key, V value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    /** The first node in this dictionary, or null if this dictionary is empty. */
    private Node front;

    @Override
    public void put(K key, V value) {
        if(value == null) { // if a null value is given, the dictionary is empty
            isEmpty();
            return;
        }

        for (Node n = front; n != null; n = n.next) {
            if (n.key.equals(key)) {
                    return;
                }
            }
       front = new Node(key, value, front); // front node now contains new key and value
    }


    @Override
    public V get(K key) {
        if (key == null) // if a null key is given, null is returned
            return null;

        for (Node n = front; n != null; n = n.next) {
            if (n.key.equals(key)) {
                return n.value; // if they key is within the contents of the dictionary, return the corresponding value
            }
        }
       return null; //if the key does not appear in the dictionary, null is returned.
    }

    @Override
    public boolean isEmpty() {
        return front == null;
    }
}

I see my problem pretty clearly. In my "put" method, if the user tries to input a null value, the entire dictionary is marked as "empty." I'm imagining this scenario:

Dictionary<Integer, String> d = new LinkedDictionary<>();
d.put(1, "one"); 
d.put(2, "two"); 
d.put(1, null); 

As soon as my implementation reaches the third line in which the key 1 is set to null, it is going to set the entire dictionary to empty, despite the fact there is still a valid entry at key 2. I've been wracking my brain over how to alter my code to allow for this sort of scenario, but I cannot figure it out. I've tried altering my isEmpty method, and I've even tried adding an additional method, but I just can't seem to figure out what to do here!

Any pointers or shoves in the right direction would be much appreciated!

public final class LinkedDictionary<K, V> {

    private Node<K, V> head;
    private Node<K, V> tail;
    private int size;
    
    public int getSize() {
        return size;
    }

    public void put(K key, V value) {
        Objects.requireNonNull(key, "Node.key");
        Node<K, V> node = new Node<>(key, value);

        remove(key);

        if (isEmpty())
            head = node;
        else
            tail.next = node;

        size++;
        tail = node;
    }

    public V remove(K key) {
        Objects.requireNonNull(key, "Node.key");

        if (isEmpty())
            return null;

        if (head.key.equals(key)) {
            V value = head.value;
            head = head.next;
            tail = head == null ? null : tail;
            size--;
            return value;
        }

        Node<K, V> prv = head;
        Node<K, V> it = head;

        while (it != null) {
            if (it.key.equals(key)) {
                V value = it.value;
                prv.next = it.next;
                it.next = null;
                size--;
                return value;
            }

            prv = it;
            it = it.next;
        }

        return null;
    }

    public V get(K key) {
        Objects.requireNonNull(key, "Node.key");
        Node<K, V> node = head;

        while (node != null) {
            if (node.key.equals(key))
                return node.value;
            node = node.next;
        }

        return null;
    }

    public boolean isEmpty() {
        return head == null;
    }

    private static class Node<K, V> {

        private final K key;
        private final V value;
        private Node<K, V> next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }
}

I'm copy yours code excluding implementation and @Override, everything works fine在此处输入图片说明

and d.isEmpty() give me a false as result

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