数据结构笔记总结(6.6) 基于链表的映射实现

代码演示

这一小节我们具体编码基于链表来实现映射。

有了之前实现链表的经验,实现映射也是比较容易的。

创建LinkedListMap.java

private class Node{
    public K key;
    public V value;
    public Node next;

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

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

    public Node(){
        this(null, null, null);
    }

    @Override
    public String toString(){
        return key.toString() + " : " + value.toString();
    }
}

private Node dummyHead;
private int size;

public LinkedListMap(){
    dummyHead = new Node();
    size = 0;
}

@Override
public int getSize(){
    return size;
}

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

private Node getNode(K key){
    Node cur = dummyHead.next;
    while(cur != null){
        if(cur.key.equals(key))
            return cur;
        cur = cur.next;
    }
    return null;
}

@Override
public boolean contains(K key){
    return getNode(key) != null;
}

@Override
public V get(K key){
    Node node = getNode(key);
    return node == null ? null : node.value;
}

@Override
public void add(K key, V value){
    Node node = getNode(key);
    if(node == null){
        dummyHead.next = new Node(key, value, dummyHead.next);
        size ++;
    }
    else
        node.value = value;
}

@Override
public void set(K key, V newValue){
    Node node = getNode(key);
    if(node == null)
        throw new IllegalArgumentException(key + " doesn't exist!");

    node.value = newValue;
}

@Override
public V remove(K key){

    Node prev = dummyHead;
    while(prev.next != null){
        if(prev.next.key.equals(key))
            break;
        prev = prev.next;
    }

    if(prev.next != null){
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        size --;
        return delNode.value;
    }

    return null;
}

以上是我们编写的映射核心代码,下面我们来简单测试一下,看傲慢与偏见里面词频

public static void main(String[] args){

    System.out.println("Pride and Prejudice");

    ArrayList words = new ArrayList<>();
    if(FileOperation.readFile("pride-and-prejudice.txt", words)) {
        System.out.println("Total words: " + words.size());

        LinkedListMap map = new LinkedListMap<>();
        for (String word : words) {
            if (map.contains(word))
                map.set(word, map.get(word) + 1);
            else
                map.add(word, 1);
        }

        System.out.println("Total different words: " + map.getSize());
        System.out.println("Frequency of PRIDE: " + map.get("pride"));
        System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
    }

    System.out.println();
}

运行程序

源码下载

[dm href=’https://www.jikewenku.com/product/1487.html’]下载地址[/dm]

导航目录

[dm href=’https://www.jikewenku.com/geeknote/2241.html’]查看导航[/dm]

本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
极客文库 » 数据结构笔记总结(6.6) 基于链表的映射实现

欢迎加入「极客文库」,成为原创作者从这里开始!

立即加入 了解更多