当前位置:网站首页>符号表
符号表
2022-08-04 04:58:00 【printf('小白');】
- 符号表最主要的目的就是将一个键和一个值联系起来,符号表能够将存储的数据元素是一个键和一个值共同组成的键值对数据,我们可以根据键来查找对应的值。
- 符号表中的键具有
唯一性
符号表实际应用
符号表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,"孙少聪");
}
边栏推荐
- 2022 software test interview questions The latest ByteDance 50 real interview questions, 15k have been won after brushing, with explanation + Q&A
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
- C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
- unity框架之缓存池
- How class only static allocation and dynamic allocation
- SQL query String field less than 10 how to check
- 42. 接雨水
- C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
- 某母婴小程序加密参数解密
- [Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
猜你喜欢
Converts XML tags to TXT format (voc conversion for yolo convenient training)
docker安装mysql与宿主机相差8小时的问题。
【云原生--Kubernetes】Pod资源管理与探针检测
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
七夕节,我用代码制作了表白信封
mysql索引笔记
Simple operation of the file system
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
SQL interview Questions
el-Select 选择器 底部固定
随机推荐
How to open a CITIC Securities online account?is it safe?
使用Patroni回调脚本绑定VIP的坑
How to simplify the automation of modern e-procurement?
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
System design. How to design a spike system (full version transfer)
7-3 LVS+Keepalived Cluster Description and Deployment
8.Haproxy 搭建Web集群
使用serve搭建本地服务器
redis中常见的面试题
[Skill] Using Sentinel to achieve priority processing of requests
For Qixi Festival, I made a confession envelope with code
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
share总结
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
小程序 + 电商,玩转新零售
【21天学习挑战赛】直接插入排序
七夕节,我用代码制作了表白信封
解决问题遇到的问题