当前位置:网站首页>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,"孙少聪");
}
边栏推荐
- 大型连锁百货运维审计用什么软件好?有哪些功能?
- [Evaluation model] Topsis method (pros and cons distance method)
- Take care of JVM performance optimization (own note version)
- redis中常见的面试题
- day13--postman接口测试
- System design. How to design a spike system (full version transfer)
- 路网编辑器技术预研
- 【技巧】借助Sentinel实现请求的优先处理
- 7-3 LVS+Keepalived Cluster Description and Deployment
- 7-2 LVS+DR概述与部署
猜你喜欢
随机推荐
drools从下载到postman请求成功
深度学习21天——准备(环境配置)
How to keep the source code confidential in the development under the burning scenario
Write golang simple C2 remote control based on gRPC
DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
7-2 LVS+DR概述与部署
[One step in place] Jenkins installation, deployment, startup (complete tutorial)
2023年PMP考试会用新版教材吗?回复来了!
docker安装mysql与宿主机相差8小时的问题。
商城系统APP如何开发 都有哪些步骤
For Qixi Festival, I made a confession envelope with code
leetcode 12. Integer to Roman numeral
OpenGL绘制一个圆锥
【SemiDrive源码分析】【MailBox核间通信】47 - 分析RPMSG_IPCC_RPC 方式 单次传输的极限大小 及 极限带宽测试
Mini program + e-commerce, fun new retail
【21天学习挑战赛】顺序查找
如何简化现代电子采购的自动化?
JVM笔记
How to dynamically add script dependent scripts
The 2022 PMP exam has been delayed, should we be happy or worried?