当前位置:网站首页>The symbol table
The symbol table
2022-08-04 05:00:00 【printf('Xiaobai');】
- 符号表最主要的目的就是将一个键和一个值联系起来,符号表能够将存储的数据元素是一个键和一个值共同组成的键值对数据,我们可以根据键来查找对应的值.
- The keys in the symbol table have
唯一性
Symbol table practical application
符号表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;
}
}
// does not exist in the symbol tablekey的键值对,Then you need to create a new tie,保存要插入的键值对,Insert the new node into the head of the two tables.
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){
// Change the current node and the previous node
pre = curr;
curr = curr.next;
}
// 如果当前结点curr的键和要插入的key一样,则替换
if (curr != null && key.compareTo(curr.key)==0){
curr.value = value;
return;
}
// key不一样,Insert the new node into itcurr之前
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;
}
}
- 测试(
View the data stored in the symbol table by debugging
)
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,"孙少聪");
}
边栏推荐
- 10 Convolutional Neural Networks for Deep Learning 3
- Explain详解与实践
- Take care of JVM performance optimization (own note version)
- if,case,for,while
- 【21天学习挑战赛】顺序查找
- [21 Days Learning Challenge] Image rotation problem (two-dimensional array)
- 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
- How to dynamically add script dependent scripts
- Basic characteristics of TL431 and oscillator circuit
- Mini program + e-commerce, fun new retail
猜你喜欢
深度学习之 10 卷积神经网络3
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
3000 words, is take you understand machine learning!
7.LVS负载均衡群集之原理叙述
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
Towards Real-Time Multi-Object Tracking (JDE)
7-1 LVS+NAT load balancing cluster, NAT mode deployment
mysql索引笔记
获取单选框选中内容
Mini program + e-commerce, fun new retail
随机推荐
Get the selected content of the radio box
触觉智能分享-SSD20X实现升级显示进度条
某母婴小程序加密参数解密
如何动态添加script依赖的脚本
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
The Shell function
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
The 2022 PMP exam has been delayed, should we be happy or worried?
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
7-1 LVS+NAT 负载均衡群集,NAT模式部署
QT 如何识别文件的编码格式
42. 接雨水
7. The principle description of LVS load balancing cluster
Converts XML tags to TXT format (voc conversion for yolo convenient training)
7-1 LVS+NAT load balancing cluster, NAT mode deployment
【云原生--Kubernetes】Pod资源管理与探针检测
[C language advanced] program environment and preprocessing
How to simplify the automation of modern e-procurement?
How to keep the source code confidential in the development under the burning scenario
Write golang simple C2 remote control based on gRPC