当前位置:网站首页>符号表
符号表
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,"孙少聪");
}
边栏推荐
- 震惊,99.9% 的同学没有真正理解字符串的不可变性
- About yolo7 and gpu
- 8. Haproxy builds a web cluster
- System design. How to design a spike system (full version transfer)
- 7.LVS负载均衡群集之原理叙述
- 深度学习21天——准备(环境配置)
- How to keep the source code confidential in the development under the burning scenario
- C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
- 离线采集怎么看sql执行计划
- 烧录场景下开发如何进行源代码保密工作
猜你喜欢

【一步到位】Jenkins的安装、部署、启动(完整教程)

drools from download to postman request success
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)

Towards Real-Time Multi-Object Tracking(JDE)

Significant differences between Oracle and Postgresql in PLSQL transaction rollback

Simple operation of the file system

SQL interview Questions

Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis

if,case,for,while
![[C language advanced] program environment and preprocessing](/img/ac/a13dd2cc47136d4938b6fc7fad660c.png)
[C language advanced] program environment and preprocessing
随机推荐
Mini program + e-commerce, fun new retail
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
How class only static allocation and dynamic allocation
系统设计.如何设计一个秒杀系统(完整版 转)
路网编辑器技术预研
How to open a CITIC Securities online account?is it safe?
Introduction and application of go module
八年软件测试工程师带你了解-测试岗进阶之路
SQL interview Questions
深度学习之 10 卷积神经网络3
QT 如何识别文件的编码格式
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
Jenkins export and import Job Pipeline
详解八大排序
如何动态添加script依赖的脚本
How to keep the source code confidential in the development under the burning scenario
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
7-1 LVS+NAT 负载均衡群集,NAT模式部署
8.Haproxy 搭建Web集群