当前位置:网站首页>符号表
符号表
2022-08-10 05:32:00 【cbys-1357】
符号表
符号表最主要的目的就是将一个键和一个值联系起来,符号表能够将存储的数据元素是一个键和一个值共同组成的 键值对数据,我们可以根据键来查找对应的值。

符号表中,键具有唯一性。
符号表在实际生活中的使用场景是非常广泛的,见下表:
应用 | 查找目的 | 键 | 值 |
字典 | 找出单词的释义 | 单词 | 释义 |
图书索引 | 找出某个术语相关的页码 | 术语 | 一串页码 |
网络搜索 | 找出某个关键字对应的网页 | 关键字 | 网页名称 |
代码实现:
public class SymbolTable<Key,Value> {
// 记录头节点
private Node head;
// 记录符号表元素的个数
private int N;
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 SymbolTable() {
this.head=new Node(null,null,null);
this.N=0;
}
// 返回符号表中元素的个数
public int size() {
return N;
}
// 向符号表中添加元素
public void put(Key key,Value value) {
Node n=head;
// 遍历符号表
while(n.next!=null) {
n=n.next;
// 如果键在链表中存在,则只需要改变key所对应值
if(n.key.equals(key)) {
n.value=value;
return;
}
}
// 如果不存在,则添加一个新结点(键值对)
Node newNode=new Node(key,value,null);
Node oldfirst=head.next;
head.next=newNode;
newNode.next=oldfirst;
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;
}
}
边栏推荐
- Chained Picks: Starbucks looks at digital collectibles and better engages customers
- 使用Google Protobuf 在 Matlab 中工作
- Linux database Oracle client installation, used for shell scripts to connect to the database with sqlplus
- 你不知道的常规流
- Multi-table query Notes
- 笔记1
- Chain Reading Good Article: Jeff Garzik Launches Web3 Production Company
- One step ahead, don't miss it again, the chain reading APP will be launched soon!
- redis---非关系型数据库(NoSql)
- I use this recruit let the team to improve the development efficiency of 100%!
猜你喜欢
随机推荐
Mini Program Study Notes: Communication between Mini Program Components
作业实验四
ResNet的基础:残差块的原理
Linux数据库Oracle客户端安装,用于shell脚本用sqlplus连接数据库
Reflection 【Notes】
I use this recruit let the team to improve the development efficiency of 100%!
小记录:Pytorch做深度学习必要加载的包
Chain Reading Recommendation: From Tiles to Generative NFTs
Models corresponding to each architecture instruction set
opencv
ZigBee 网络设备相关内容
常用类 String概述
Operation table Function usage
网络安全作业
十年磨一剑!数字藏品行情软件,链读APP正式开放内测!
PCL点云滤波
Collection工具类
各个架构指令集对应的机型
复杂的“元宇宙”,为您解读,链读APP即将上线!
智能合约和去中心化应用DAPP









