第三章 数据结构
3.2 符号表
符号表是存储键及对应值的数据结构,符号表中存储的元素由键,值和指向下一个值的指针域组成,可通过键查找到对应的值。
符号表中,键必须是唯一的,而值可以不唯一。
日常生活中,根据关键字百度查找资料,根据目录查找书籍内容,都是符号表使用的体现。
链表实现API :
结点类:
类名 | Node<K,V> |
构造 | Node<K k, V v, Node next> 创建Node对象 |
成员变量 | public K key; 存储键,必须唯一 public V value;存储值, public Node next;存储下 |
符号表:
类名 | SymbolTable<K,V> |
构造 | SymbolTable() 创建Node对象 |
成员方法 | public V get(K k) 根据key返回value public void put(K key,Value value) 插入键值对 public void delete(Key key) 删除键为Key的键值对 public int size() 键值对的个数 |
成员变量 | public Node head; 存储键,必须唯一 public int N;存储值 |
编码实现 :
public class SymbolTable<Key,Value> {
private int N;
private Node head;
public SymbolTable() {
this.N = 0;
this.head = new Node(null,null,null);
}
public int size() {
return N;
}
/**
* 插入新数据 :
* @param key
* @param value
*/
public void put(Key key,Value value) {
//key存在update 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);
newNode.next = head.next;
head.next = newNode;
N++;
}
/**
* 返回Value
* @param key
* @return
*/
public Value get(Key key) {
Node node = head;
while (node.next != null) {
node = node.next;
if (node.key.equals(key)) {
return node.value;
}
}
return null;
}
/**
* 删除
* @param key
*/
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.next;
}
}
/**
* 结点类
*/
public class Node {
public Key key;
public Value value;
public Node next;
public Node(Key k, Value v, Node next) {
this.key = k;
this.value = v;
this.next = next;
}
}
}
测试代码
public class TestSymbolTable {
public static void main(String[] args) {
SymbolTable<Integer, String> symbolTable = new SymbolTable<Integer, String>();
symbolTable.put(1,"1111");
symbolTable.put(2,"22222");
symbolTable.put(3,"33");
System.out.println(symbolTable.size());
System.out.println(symbolTable.get(2));
symbolTable.put(2,"BBBBBB");
System.out.println(symbolTable.get(2));
System.out.println(symbolTable.size());
}
}
有序符号表
public class OrderSymbolTable<Key extends Comparable,Value> implements Iterable<Key> {
private int N;
private Node head;
public OrderSymbolTable() {
this.N = 0;
this.head = new Node(null,null,null);
}
public int size() {
return N;
}
/**
* 插入新数据 : 有序插入
*
* @param key
* @param value
*/
public void put(Key key,Value value) {
Node prior = head;
Node current = head.next;
if (current == null) {
head.next = new Node(key,value,null);
N++;
return;
}
while (current != null) {
if (current.key.compareTo(key) < 0) {
prior = current;
current = current.next;
continue;
}
if (current.key.compareTo(key) == 0) {
current.value = value;
return;
}
break;
}
Node newNode = new Node(key, value, current);
prior.next = newNode;
N++;
}
/**
* 返回Value
* @param key
* @return
*/
public Value get(Key key) {
Node node = head;
while (node.next != null) {
node = node.next;
if (node.key.equals(key)) {
return node.value;
}
}
return null;
}
/**
* 删除
* @param key
*/
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.next;
}
}
/**
* 实现迭代
* @return
*/
public Iterator<Key> iterator() {
return new SymbolTableIterator();
}
public class SymbolTableIterator implements Iterator {
private Node node;
public SymbolTableIterator() {
this.node = head;
}
public boolean hasNext() {
return node.next != null;
}
public Key next() {
node = node.next;
return node.key;
}
public void remove() {
}
}
/**
* 结点类
*/
public class Node {
public Key key;
public Value value;
public Node next;
public Node(Key k, Value v, Node next) {
this.key = k;
this.value = v;
this.next = next;
}
}
}
测试代码
public class TestOrderSymbolTable {
public static void main(String[] args) {
OrderSymbolTable<Integer, String> orderSymbolTable
= new OrderSymbolTable<Integer, String>();
orderSymbolTable.put(2,"2222");
orderSymbolTable.put(3,"3333");
orderSymbolTable.put(4,"4444");
orderSymbolTable.put(1,"11111");
orderSymbolTable.put(3,"xixixi");
System.out.println(orderSymbolTable.size());
for (Integer integer : orderSymbolTable) {
System.out.println("key:"+ integer + ",value:"+ orderSymbolTable.get(integer));
}
System.out.println("------------删除后遍历----");
System.out.println(orderSymbolTable.size());
orderSymbolTable.delete(2);
for (Integer integer : orderSymbolTable) {
System.out.println("key:"+ integer + ",value:"+ orderSymbolTable.get(integer));
}
}
}