当前位置:网站首页>符号表
符号表
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;
}
}
边栏推荐
猜你喜欢
I use this recruit let the team to improve the development efficiency of 100%!
view【】【】【】【】
ORACLE系统表空间SYSTEM占满无法扩充表空间问题解决过程
MySql 约束
Chain Reading Good Article: Jeff Garzik Launches Web3 Production Company
Reflection 【Notes】
操作表 函数的使用
小记录:Pytorch做深度学习必要加载的包
The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
基本比例尺标准分幅编号流程
随机推荐
ACID四种特性
网络安全3
Minio分布式存储系统
error in ./node_modules/cesium/Source/ThirdParty/zip.js
redis集群模式
matlab中的常用的类型转换
来亲自手搭一个ResNet18网络
el-cascader级联选择器的子菜单双击两次才显示被选中的内容
清览题库--C语言程序设计第五版编程题解析(1)
Knowledge Distillation Thesis Learning
十年磨一剑!数字藏品行情软件,链读APP正式开放内测!
Notes 1
常用类 String概述
Consensus calculation and incentive mechanism
tinymce富文本编辑器
转载fstream,ifstream的详细用法
小记录:Pytorch做深度学习必要加载的包
MySql constraints
Chain Reading Recommendation: From Tiles to Generative NFTs
使用Tenserboard可视化深度学习训练过程