数据结构——链表数组

链表数组

  • 链表数组是什么?
  • 链表数组实现

链表数组是什么?

链表数组是由链表构成的数组,即数组每一个元素都是链表

链表数组实现

  • getBucket()根据hashcode获取桶数
  • Node数组元素节点

put()获取桶数,判断桶里是否有元素

  • 若无则存到桶内
  • 若有则循环判断当前key是否存在,若存在则覆盖value,不存在则进行链接到末尾

remove()获取桶数,桶里是否有元素,若无则直接返回null,若有则再判断第一个元素是否是要删除的元素

  • 若是则断开头节点,让下一个元素成为头节点
  • 若不是则循环遍历链表,判断是否有对应元素,若有且其为最后一个,则直接断开,若有但不是最后一个则断开并链接下一个
class MyLinkListArray {
    private int capacity = 5;
    Node[] tables;
    
    MyLinkListArray() {
        tables = new Node[capacity];
    }
    
    private int getBucket(int hashcode) {
        return hashcode % capacity;
    }
    
    private static class Node {
        E key;
        E value;
        Node next;
        Node(E key, E value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    public void put(E key, E value) {
        int bucket = getBucket(key.hashCode());
        Node firstNode = tables[bucket];
        if (firstNode == null) {
            tables[bucket] = new Node<>(key, value, null);
        } else {
            while (firstNode.next != null) {
                if (firstNode.key == key) {
                    firstNode.value = value;
                    return;
                }
                firstNode = firstNode.next;
            }
            firstNode.next = new Node<>(key, value, null);
        }
    }

    public void remove(E key) {
        int bucket = getBucket(key.hashCode());
        Node firstNode = tables[bucket];
        if (firstNode == null) {
            return;
        }
        if (firstNode.key == key) {
            Node nextNode = firstNode.next;
            firstNode.next = null;
            tables[bucket] = nextNode;
        } else {
            while (firstNode.next != null) {
                Node nextNode = firstNode.next;
                if (nextNode.key == key) {
                    if (nextNode.next != null) {
                        Node temp = nextNode.next;
                        nextNode.next = null;
                        firstNode.next = temp;
                    } else {
                        firstNode.next = null;
                    }
                } else {
                    firstNode = nextNode;
                }
            }
        }
    }

    public E get(E key) {
        int bucket = getBucket(key.hashCode());
        Node firstNode = tables[bucket];
        if (firstNode == null) {
            return null;
        }
        if (firstNode.key == key) {
            return firstNode.value;
        } else {
            while (firstNode.next != null) {
                firstNode = firstNode.next;
                if (firstNode.key == key) {
                    return firstNode.value;
                }
            }
        }
        return null;
    }

    public void set(E key, E value) {
        int bucket = getBucket(key.hashCode());
        Node firstNode = tables[bucket];
        if (firstNode.key == key) {
            firstNode.value = value;
        } else {
            while (firstNode.next != null) {
                firstNode = firstNode.next;
                if (firstNode.key == key) {
                    firstNode.value = value;
                }
            }
        }
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < capacity; i++) {
            Node firstNode = tables[i];
            if (firstNode != null) {
                stringBuilder.append("[" + firstNode.key + "," + firstNode.value + "]");
                while (firstNode.next != null) {
                    firstNode = firstNode.next;
                    stringBuilder.append("-[" + firstNode.key + "," + firstNode.value + "]");
                }
                stringBuilder.append(", ");
            }
        }
        stringBuilder.replace(stringBuilder.length() - 2, stringBuilder.length(), "");
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}

get()获取桶数,并判断桶内是否有元素,若无则返回null,若有则判断是否是第一个元素

  • 若是则返回头节点的value
  • 若不是则循环遍历查找是否有对应的key并返回其value

set获取桶数,判断是否是第一个元素

  • 若是则设置头节点的value
  • 若不是则循环遍历查找是否有对应的key并设置其value

测试代码,toString打印如[[0,0], [1,1]-[6,6]-[11,11], [2,2]]

MyLinkListArray myLinkListArray = new MyLinkListArray<>();
myLinkListArray.put(0, 0);
myLinkListArray.put(1, 1);
myLinkListArray.put(2, 2);
myLinkListArray.put(6, 6);
myLinkListArray.put(11, 11);
System.out.println(myLinkListArray);
System.out.println(myLinkListArray.get(1));
System.out.println(myLinkListArray.get(6));
System.out.println(myLinkListArray.get(3));
myLinkListArray.set(6, 7);
System.out.println(myLinkListArray);
myLinkListArray.remove(0);
System.out.println(myLinkListArray);
myLinkListArray.remove(6);
System.out.println(myLinkListArray);

你可能感兴趣的:(#,数据结构,链表,数据结构,java)