算法与数据结构(四):符号表

算法与数据结构(四):符号表

  • 博主会对算法与数据结构会不断进行更新,敬请期待,如有什么建议,欢迎联系。

  • 符号表最主要的目的就是将一个键和一个值联系起来,符号表能够将存储的数据元素是一个键和一个值共同组成的键值对数据,我们可以根据键来查找对应的值。

  • 符号表是最简单的键值对集合,是Hashmap最简单的底层实现,底层可以由数组或链表实现,在这里博主使用链表进行了实现。

  • 符号表的实现代码如下:


package com.victor.symbol;

import java.io.Serializable;

/**
 * @Description: 符号表
 * @Author: victor
 */
public class OrderSymbolTable<K extends Comparable<K>, V> implements Serializable {

    private static final long serialVersionUID = -3107896310224392365L;
    //记录首节点 -3107896310224392365L
    private Node head;
    //记录元素的个数
    private int N;

    //内部类节点,为了测试所以访问修饰符为public
    public 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;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "key=" + key +
                    ", value=" + value +
                    ", next=" + next +
                    '}';
        }
    }

    public OrderSymbolTable() {
        head = new Node(null, null, null);
        N = 0;
    }

    /**
     * 获得符号表中键值对的个数
     *
     * @return 键值对的个数
     */
    public int size() {
        return N;
    }

    /**
     * 插入一个数据
     *
     * @param key   键
     * @param value 值
     */
    public void put(K key, V value) {
        Node n = head;
        //如果还没有元素的话插入第一个元素
        if (N == 0) {
            n.next = new Node(key, value, null);
            N++;
            return;
        }
        //先让指针移动到相应的位置
        while (n.next != null && key.compareTo(n.next.key) > 0) {
            n = n.next;
        }
        //如果下一个节点和key相等则替换
        if (n.next != null && key.compareTo(n.next.key) == 0) {
            n.next.value = value;
            return;
        }
        //插入到相应的位置
        if (n.next != null && key.compareTo(n.next.key) < 0) {
            n.next = new Node(key, value, n.next);
            N++;
            return;
        }
        //指针移动到了最后
        n.next = new Node(key, value, null);
        N++;
    }

    /**
     * 删除一个键值对
     *
     * @param key 键
     */
    public void delete(K key) {
        Node n = head;
        while (n.next != null) {
            if (n.next.key.equals(key)) {
                n.next = n.next.next;
                N--;
                return;
            }
            n = n.next;
        }
    }

    /**
     * 获取key对应的值
     *
     * @param key 键
     * @return 值
     */
    public V get(K key) {
        Node n = head;
        while (n.next != null) {
            n = n.next;
            if (n.key.equals(key)) {
                return n.value;
            }
        }
        return null;
    }

    /**
     * 用于测试拿到node
     *
     * @param key 键
     * @return node节点
     */
    public Node getNode(K key) {
        Node n = head;
        while (n.next != null) {
            n = n.next;
            if (n.key.equals(key)) {
                return n;
            }
        }
        return null;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值