预见猿份
主题
首页面试题在线工具关于我们老苗一对一私教学员评价
实战项目
项目前置基础创新WMS项目Java微服务框架与实战云岚到家项目闪聚支付项目学成在线项目青橙电商项目JVM原理与实战调优分布式事务专题Java高频面试题MySQL从入门到精通Java数据结构与算法老苗一对一私教学员评价blog
blog
  • Java 数据结构与算法课程|从原理到手写代码,搞定面试算法题

    • 课程介绍
    • 01 数据结构和算法概述
    • 02 算法分析
    • 03 排序
    • 04 线性表
    • 05 符号表
    • 06 树的入门
    • 07 堆
    • 08 优先队列
    • 09 树的进阶
    • 10 并查集
    • 11 图的入门
    • 12 图的进阶







----- 到底线了 -----

×

欢迎来到预见猿份,本站项目均为站长原创,学习中有问题可直接提交给站长老苗解决(微信:mrt_0607)。

苗润土老师,20余年一线项目经验,2014年加入黑马,星辰wms、云岚到家、学成在线项目作者,历任高级讲师、教学主管及课程研究员。 b站老苗

05符号表 ​

一、符号表 ​

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

符号表中,键具有唯一性。

符号表在实际生活中的使用场景是非常广泛的,见下表:

应用查找目的键值
字典找出单词的释义单词释义
图书索引找出某个术语相关的页码术语一串页码
网络搜索找出某个关键字对应的网页关键字网页名称

1.1 符号表API设计 ​

结点类:

类名Node<Key,Value>
构造方法Node(Key key,Value value,Node next):创建Node对象
成员变量1.public Key key:存储键
2.public Value value:存储值
3.public Node next:存储下一个结点

符号表:

类名SymbolTable<Key,Value>
构造方法SymbolTable():创建SymbolTable对象
成员方法1.public Value get(Key key):根据键key,找对应的值
2.public void put(Key key,Value val):向符号表中插入一个键值对
3.public void delete(Key key):删除键为key的键值对
4.public int size():获取符号表的大小
成员变量1.private Node head:记录首结点
2.private int N:记录符号表中键值对的个数

1.2 符号表实现 ​

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

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

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

    //往符号表中插入键值对
    public void put(Key key,Value value){
        //先从符号表中查找键为key的键值对
        Node n = head;
        while(n.next!=null){
            n = n.next;
            if (n.key.equals(key)){
                n.value=value;
                return;
            }
        }

        //符号表中没有键为key的键值对
        Node oldFirst = head.next;
        Node newFirst = new Node(key,value,oldFirst);
        head.next = newFirst;
        //个数+1
        N++;
    }
    //删除符号表中键为key的键值对
    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.next;
        }
    }

    //从符号表中获取key对应的值
    public Value get(Key key){
        Node n = head;
        while(n.next!=null){
            n = n.next;
            if (n.key.equals(key)){
                return n.value;
            }
        }
        return null;
    }

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

        public Node(Key key, Value value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
}

//测试类
public class Test {
   public static void main(String[] args) throws Exception {
        SymbolTable<Integer, String> st = new SymbolTable<>();
        st.put(1, "张三");
        st.put(3, "李四");
        st.put(5, "王五");
        System.out.println(st.size());
        st.put(1,"老三");
        System.out.println(st.get(1));
        System.out.println(st.size());
        st.delete(1);
        System.out.println(st.size());
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

1.3 有序符号表 ​

刚才实现的符号表,我们可以称之为无序符号表,因为在插入的时候,并没有考虑键值对的顺序,而在实际生活中,有时候我们需要根据键的大小进行排序,插入数据时要考虑顺序,那么接下来我们就实现一下有序符号表。

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

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

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

    //往符号表中插入键值对
    public void put(Key key,Value value){
        //记录当前结点
        Node curr = head.next;
        //记录上一个结点
        Node pre = head;
        //1.如果key大于当前结点的key,则一直寻找下一个结点
        while(curr!=null && key.compareTo(curr.key)>0){
            pre = curr;
            curr = curr.next;
        }
        //2.如果当前结点curr的key和将要插入的key一样,则替换
        if (curr!=null && curr.key.compareTo(key)==0){
            curr.value=value;
            return;
        }
        //3.没有找到相同的key,把新结点插入到curr之前
        Node newNode = new Node(key, value, curr);
        pre.next = newNode;
    }
    //删除符号表中键为key的键值对
    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.next;
        }
    }

    //从符号表中获取key对应的值
    public Value get(Key key){
        Node n = head;
        while(n.next!=null){
            n = n.next;
            if (n.key.equals(key)){
                return n.value;
            }
        }
        return null;
    }


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

        public Node(Key key, Value value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
}

//测试代码
public class Test {
    public static void main(String[] args) throws Exception {
        OrderSymbolTable<Integer, String> bt = new OrderSymbolTable<>();
        bt.put(4, "二哈");
        bt.put(3, "张三");
        bt.put(1, "李四");
        bt.put(1, "aa");
        bt.put(5, "王五");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
← 04 线性表06 树的入门 →








如果发现文档内容有错误或排版错乱,请及时联系站长老苗修改,不胜感激。联系方式
关于我们 | 隐私政策 | 豫ICP备2026003386号-4 | 豫公网安备41010202004008号
目录

本页无章节