2、符号表实现
package cn.itcast.algorithm.symbol; 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 = n.next; //判断n结点存储的键是否为key,如果是,则替换n结点的值 if (n.key.equals(key)){ n.value = value; return; } } //如果符号表中不存在键为key的键值对,只需要创建新的结点,保存要插入的键值对,把新结点插入到链表的头部 head.next=新结点即可 Node newNode = new Node(key, value, null); Node oldFirst = head.next; newNode.next = oldFirst; head.next = newNode; //元素个数+1; 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 = n.next; if (n.key.equals(key)){ return n.value; } } return null; } }
package cn.itcast.algorithm.test; import cn.itcast.algorithm.symbol.SymbolTable; public class SymbolTableTest { public static void main(String[] args) { //创建符号表对象 SymbolTable<Integer, String> symbolTable = new SymbolTable<>(); //测试put方法(插入,替换) symbolTable.put(1,"乔峰"); symbolTable.put(2,"虚竹"); symbolTable.put(3,"段誉"); System.out.println("插入完毕后,元素的个数为:"+symbolTable.size()); symbolTable.put(2, "慕容复"); System.out.println("替换完毕后的元素的个数为:"+symbolTable.size()); //测试get方法 System.out.println("替换完毕后,键2对应的值为:"+symbolTable.get(2)); //测试删除方法 symbolTable.delete(2); System.out.println("删除完毕后,元素的个数:"+symbolTable.size()); } }
3、有序符号表
package cn.itcast.algorithm.symbol; 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变量,分别记录当前结点和当前结点的上一个结点 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; return; } //如果当前结点curr的键和要插入的key不一样,把新的结点插入到curr之前 Node newNode = new Node(key, value, curr); pre.next = newNode; //元素的个数+1; 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 = n.next; if (n.key.equals(key)){ return n.value; } } return null; } }
package cn.itcast.algorithm.test; import cn.itcast.algorithm.symbol.OrderSymbolTable; public class OrderSymbolTableTest { public static void main(String[] args) { //创建有序符号表对象 OrderSymbolTable<Integer, String> table = new OrderSymbolTable<>(); table.put(1,"张三"); table.put(2,"李四"); table.put(4,"赵六"); table.put(7,"田七"); table.put(3,"王五"); } }