1.知识储备
2.构建API:
3.代码实现:
package Symbol;
import java.util.Iterator;
public class symbolTable<key extends Comparable<key>,value> implements Iterable<value>{
//头节点
private Node head;
//记录符号表中元素个数
private int N;
//构造方法,初始化符号表
public symbolTable() {
// TODO Auto-generated constructor stub
this.head=new Node(null,null,null);
this.N=0;
}
//节点类
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 int size(){
return N;
}
//根据key值查找对应的值
public value get(key key){
//找到key所对应的节点
Node n=head;
while(n.next!=null){
if(n.next.key.equals(key)){
return n.next.value;
}