一、符号表
符号表最主要的目的就是将一个键和一个值联系起来,符号表能够将存储的数据是一个键和一个值共同组成的键值对数据,可以根据键来查找对应的值
1.1符号表API设计
结点类:
类名 | Node<Key,Value> |
---|---|
构造方法 | Node(Key key,Value value,Node next):创建node对象 |
成员变量 | 1.public Key key:存储键 2.public Value value:存储值 3.public Node next:存储下一个结点 |
符号表:
类名 | SymbolTable<Key,Value> |
---|---|
构造方法 | SymbolTable():创建SymbolTable对象 |
成员方法 | 1.public Value get(Key key):根据键找对应值 2.public void put(Key key,Value val):向符号表中插入一个键值对 3.public void delete(Key key):删除键key为key的键值对 4.public int size():获取符号表的大小 |
成员变量 | 1.private Node head:记录首结点 2.private int N:记录符号表中键值对的个数 |
代码实现:
public class SymbolTable <Key,Value>{
private Node head;
private int N;
private class Node{
public Key key;
public 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;
newnode.next = oldfirst;
head.next = newnode;
N++;
}
//删除符号表中键为key的键值对
public void delete (Key key){
//找到键为key的结点,把该结点从链表中删除
Node n = head;
while (n.next != null){
//判断n结点的下一个结点的键是否为key,如果是就删除该结点
if(n.next.key.equals(key)){
n.next = n.next.next;
N--;
return;
}
//变换n
n = n.next;
}
}
//从符号表中获取key对应的值
public Value get(Key key){
//找到键为key的结点
Node n = head;
while (n.next != null){
n=n.next;
if(n.key.equals(key)){
return n.value;
}
}
return null;
}
}
//测试代码
import algorithm.symbol.SymbolTable;
public class SymbolTableTest {
public static void main(String[] args) {
SymbolTable<Integer,String> symbolTable = new SymbolTable<>();
symbolTable.put(1,"a");
symbolTable.put(2,"b");
symbolTable.put(3,"c");
symbolTable.put(4,"d");
System.out.println("插入后元素个数"+symbolTable.size());
symbolTable.put(2,"y");
System.out.println("替换后的元素个数"+symbolTable.size());
System.out.println("替换后键2对应的值"+symbolTable.get(2));
symbolTable.delete(2);
System.out.println("删除后的元素个数"+symbolTable.size());
}
}
有序符号表代码实现:
public class OrderSymbolTable<Key extends Comparable<Key>,Value>{
private Node head;
private int N;
private class Node{
public Key key;
public 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){
//定义两个变量,分别记录当前结点和当前结点的上一个结点
Node curr = head.next;
Node pre = head;
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;
}
//如果当前结点curr的键和要插入的key的key不一样,把新的结点插入到cyrr之前
Node newNode = new Node(key,value,curr);
pre.next = newNode;
N++;
}
//删除符号表中键为key的键值对
public void delete (Key key){
//找到键为key的结点,把该结点从链表中删除
Node n = head;
while (n.next != null){
//判断n结点的下一个结点的键是否为key,如果是就删除该结点
if(n.next.key.equals(key)){
n.next = n.next.next;
N--;
return;
}
//变换n
n = n.next;
}
}
//从符号表中获取key对应的值
public Value get(Key key){
//找到键为key的结点
Node n = head;
while (n.next != null){
n=n.next;
if(n.key.equals(key)){
return n.value;
}
}
return null;
}
}