Skip to content

Commit 11a366e

Browse files
committed
[Function add]
1.Sequential symbol table.
1 parent 05f71d8 commit 11a366e

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Diff for: Algorithm(4th_Edition)/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre"/>
5+
<classpathentry kind="lib" path="lib/algs4.jar"/>
56
<classpathentry kind="output" path="bin"/>
67
</classpath>

Diff for: Algorithm(4th_Edition)/algorithm_note.txt

+49
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,57 @@
719719
}
720720
}
721721

722+
-------------------------------------------------------------------------------------------------------------------------
723+
---------------------------------------Chapter Three---------------------------------------------------------------------
724+
-------------------------------------------------------------------------------------------------------------------------
725+
1. 符号表Symbol Table:
726+
符号表的主要目的就是将一个键和一个值联系起来。
727+
->重复的键:
728+
1.每个键都对应一个值。
729+
2.存入的键值对和已有的键起冲突时会覆盖旧的值。
730+
->空键:
731+
1.键不能为空。
732+
->空值:
733+
1.值不能为空。
734+
2.可以通过get()方法返回值是不是null,来判断值是不是已经被删除。
735+
3.put()空值用于删除。
722736

737+
2. 通过链表实现的符号表:
738+
实现:ca.mcmaster.chapter.three.SequentialSearchST<K, V>
739+
这种符号表是无序的,属于随机命中。
740+
public class SequentialSearchST<K, V> {
741+
private class Node{
742+
K k;
743+
V v;
744+
Node next;
745+
public Node(K k, V v, Node next){
746+
this.k = k;
747+
this.v = v;
748+
this.next = next;
749+
}
750+
}
751+
private Node first;
752+
public V get(K k){
753+
Node current = first;
754+
while(current != null){
755+
if(current.k.equals(k)) return current.v;
756+
current = current.next;
757+
}
758+
return null;
759+
}
723760

761+
public void put(K k, V v){
762+
Node current = first;
763+
while(current != null){
764+
if(current.k.equals(k)){
765+
current.v = v;
766+
return;
767+
}
768+
current = current.next;
769+
}
770+
first = new Node(k, v, first);
771+
}
772+
}
724773

725774

726775

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ca.mcmaster.chapter.three;
2+
3+
public class SequentialSearchST<K, V> {
4+
private class Node{
5+
K k;
6+
V v;
7+
Node next;
8+
public Node(K k, V v, Node next){
9+
this.k = k;
10+
this.v = v;
11+
this.next = next;
12+
}
13+
}
14+
private Node first;
15+
public V get(K k){
16+
Node current = first;
17+
while(current != null){
18+
if(current.k.equals(k)) return current.v;
19+
current = current.next;
20+
}
21+
return null;
22+
}
23+
24+
public void put(K k, V v){
25+
Node current = first;
26+
while(current != null){
27+
if(current.k.equals(k)){
28+
current.v = v;
29+
return;
30+
}
31+
current = current.next;
32+
}
33+
first = new Node(k, v, first);
34+
}
35+
36+
public static void main(String[] args) {
37+
SequentialSearchST<String, String> table = new SequentialSearchST<>();
38+
table.put("a", "aaaa");
39+
table.put("b", "bbbb");
40+
table.put("c", "cccc");
41+
table.put("d", "dddd");
42+
System.out.println(table.get("a"));
43+
System.out.println(table.get("b"));
44+
System.out.println(table.get("c"));
45+
System.out.println(table.get("d"));
46+
table.put("a", "1111");
47+
System.out.println(table.get("a"));
48+
System.out.println(table.get("p"));
49+
}
50+
}

0 commit comments

Comments
 (0)