当前位置:网站首页>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,"孙少聪");
}
边栏推荐
- 【21天学习挑战赛】顺序查找
- 有趣的 Kotlin 0x0E:DeepRecursiveFunction
- 深度学习之 10 卷积神经网络3
- DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
- unity框架之缓存池
- System design. How to design a spike system (full version transfer)
- 看DevExpress丰富图表样式,如何为基金公司业务创新赋能
- JVM Notes
- Uni-app 小程序 App 的广告变现之路:全屏视频广告
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
猜你喜欢
随机推荐
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
小程序 + 电商,玩转新零售
SVM介绍以及实战
drools从下载到postman请求成功
解决问题遇到的问题
day13--postman接口测试
【21天学习挑战赛】图像的旋转问题(二维数组)
C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
[One step in place] Jenkins installation, deployment, startup (complete tutorial)
Converts XML tags to TXT format (voc conversion for yolo convenient training)
42. 接雨水
Write golang simple C2 remote control based on gRPC
Explain detailed explanation and practice
What are the functions of mall App development?
深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
[C language advanced] program environment and preprocessing
Use serve to build a local server
Bolb analysis of image processing (1)
Mini program + e-commerce, fun new retail









