当前位置:网站首页>符号表
符号表
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,"孙少聪");
}
边栏推荐
- 7.LVS负载均衡群集之原理叙述
- [C language advanced] program environment and preprocessing
- 信息学奥赛一本通 1312:【例3.4】昆虫繁殖
- Tensors - Application Cases
- 3000 words, is take you understand machine learning!
- 中信证券网上开户怎么开的?安全吗?
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
- Write golang simple C2 remote control based on gRPC
- How to open a CITIC Securities online account?is it safe?
- 7-1 LVS+NAT load balancing cluster, NAT mode deployment
猜你喜欢
如何简化现代电子采购的自动化?
【评价类模型】Topsis法(优劣解距离法)
For Qixi Festival, I made a confession envelope with code
文件内容的操作
[C language advanced] program environment and preprocessing
解决错误:npm WARN config global `--global`, `--local` are deprecated
leetcode 12. Integer to Roman numeral
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
System design. Seckill system
Shocked, 99.9% of the students didn't really understand the immutability of strings
随机推荐
【技巧】借助Sentinel实现请求的优先处理
备份工具pg_dump的使用《postgres》
解决问题遇到的问题
Structure function exercise
数据治理平台项目总结和分析
Introduction and application of go module
Basic characteristics of TL431 and oscillator circuit
Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
2003. 每棵子树内缺失的最小基因值 DFS
小程序 + 电商,玩转新零售
如何动态添加script依赖的脚本
深度学习环境配置
Tensors - Application Cases
如何简化现代电子采购的自动化?
2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
Hangdian Multi-School-Slipper- (tree map conversion + virtual point mapping)
7-2 LVS+DR概述与部署
go module的介绍与应用
mysql index notes
7. The principle description of LVS load balancing cluster