符号表

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

符号表实际应用

在这里插入图片描述

符号表API设计

节点类API

在这里插入图片描述

符号表API

在这里插入图片描述

package symbol;

/**
 * @ClassName SymbolTable
 * @Authoc 孙少聪
 * @Date 2022/8/3 09:32:37
 */

public class SymbolTable<Key,Value> {
    // 记录首节点
    private Node head;
    // 记录符号表中元素的个数
    private int N;

    private class Node{
        // 键
        private Key key;
        // 值
        private Value value;
        // 下一个结点
        public Node next;

        public Node(Key key, Value value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
    public SymbolTable(){
        this.head = new Node(null,null,null);
        this.N = 0;
    }

    public int size(){
        return N;
    }

    public void put(Key key,Value value){
        // 符号表中已经存在键为key的键值对,那么只需要找到该结点,替换为value的即可
        Node  n = head;
        while (n.next!=null){
            n = n.next;
            if(n.key.equals(key)){
                n.value = value;
                return;
            }
        }
        // 符号表中不存在key的键值对,则需要创建一个新的系欸但,保存要插入的键值对,把新节点插入到俩表的头部。
        Node newNode = new Node(key, value, null);
        Node oldFirst = head.next;
        head.next = newNode;
        newNode.next = oldFirst;
        // 元素个数加1
        N++;
    }

    public void delete(Key key){
        Node  n = head;
        while (n.next!=null){
            if(n.next.key.equals(key)){
                n.next = n.next.next;
                N--;
                return;
            }
            // 变换n
            n = n.next;
        }

    }

    public Value get(Key key){
        Node  n = head;
        while (n.next!=null){
            // 变换n
            n = n.next;
            if(n.key.equals(key)){
                return n.value;
            }
        }
        return null;
    }
}

  • 测试
    public static void main(String[] args) {
        // 创建符号表对象
        SymbolTable<Integer, String> symbolTable = new SymbolTable<>();
        // 调试put对象s
        symbolTable.put(1,"乔峰");
        symbolTable.put(2,"虚竹");
        symbolTable.put(3,"段誉");
        System.out.println(symbolTable.size());
        symbolTable.put(1,"孙少聪");
        System.out.println(symbolTable.size());

        // get方法
        System.out.println(symbolTable.get(1));

        // delete
        symbolTable.delete(2);
        System.out.println(symbolTable.size());
    }

有序符号表

package symbol;

/**
 * @ClassName SymbolTable
 * @Authoc 孙少聪
 * @Date 2022/8/3 09:32:37
 */

public class OrderSymbolTable<Key extends Comparable<Key>,Value> {
    // 记录首节点
    private Node head;
    // 记录符号表中元素的个数
    private int N;

    private class Node{
        // 键
        private Key key;
        // 值
        private Value value;
        // 下一个结点
        public Node next;

        public Node(Key key, Value value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
    public OrderSymbolTable(){
        this.head = new Node(null,null,null);
        this.N = 0;
    }

    public int size(){
        return N;
    }

    public void put(Key key,Value value){
        // 符号表中已经存在键为key的键值对,那么只需要找到该结点,替换为value的即可
        Node  pre = head;
        Node  curr = head.next;
        while (curr != null && key.compareTo(curr.key)>0){
            // 变化当前结点和前一个结点即可
            pre = curr;
            curr = curr.next;
        }
        // 如果当前结点curr的键和要插入的key一样,则替换
        if (curr != null && key.compareTo(curr.key)==0){
            curr.value = value;
            return;
        }
        // key不一样,吧新节点插入到curr之前
        Node newNode = new Node(key, value, null);
        pre.next = newNode;
        newNode.next = curr;
        // 元素个数加1
        N++;
    }

    public void delete(Key key){
        Node  n = head;
        while (n.next!=null){
            if(n.next.key.equals(key)){
                n.next = n.next.next;
                N--;
                return;
            }
            // 变换n
            n = n.next;
        }

    }

    public Value get(Key key){
        Node  n = head;
        while (n.next!=null){
            // 变换n
            n = n.next;
            if(n.key.equals(key)){
                return n.value;
            }
        }
        return null;
    }
}

  • 测试(通过调试查看符号表内存储的数据
   public static void main(String[] args) {
        // 创建符号表对象
        OrderSymbolTable<Integer, String> symbolTable = new OrderSymbolTable<>();
        // 调试put对象s
        symbolTable.put(1,"乔峰");
        symbolTable.put(2,"虚竹");
        symbolTable.put(4,"段誉");
        symbolTable.put(3,"孙少聪");

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值