当前位置:网站首页>符号表
符号表
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,"孙少聪");
}
边栏推荐
- How to keep the source code confidential in the development under the burning scenario
- System design. How to design a spike system (full version transfer)
- 小程序 + 电商,玩转新零售
- Interesting Kotlin 0x0E: DeepRecursiveFunction
- 【21天学习挑战赛】顺序查找
- [Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
- 8. Haproxy builds a web cluster
- QT 如何识别文件的编码格式
- [Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
- SQL interview Questions
猜你喜欢
【21天学习挑战赛】图像的旋转问题(二维数组)
Basic characteristics of TL431 and oscillator circuit
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
【云原生--Kubernetes】Pod资源管理与探针检测
【C语言进阶】程序环境和预处理
Reproduce 20-character short domain name bypass
How to simplify the automation of modern e-procurement?
解决错误:npm WARN config global `--global`, `--local` are deprecated
About yolo7 and gpu
深度学习之 10 卷积神经网络3
随机推荐
详解八大排序
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
转:管理是对可能性的热爱,管理者要有闯进未知的勇气
JVM笔记
3000字,一文带你搞懂机器学习!
What are the steps for how to develop a mall system APP?
2003. 每棵子树内缺失的最小基因值 DFS
【一步到位】Jenkins的安装、部署、启动(完整教程)
Reproduce 20-character short domain name bypass
Mobile payment online and offline payment scenarios
杭电多校-Slipper-(树图转化+虚点建图)
使用Patroni回调脚本绑定VIP的坑
震惊,99.9% 的同学没有真正理解字符串的不可变性
7. The principle description of LVS load balancing cluster
C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
某母婴小程序加密参数解密
有趣的 Kotlin 0x0E:DeepRecursiveFunction
【SemiDrive源码分析】【MailBox核间通信】47 - 分析RPMSG_IPCC_RPC 方式 单次传输的极限大小 及 极限带宽测试
How to simplify the automation of modern e-procurement?
go module的介绍与应用