-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequentialSearchST.java
110 lines (94 loc) · 3.18 KB
/
SequentialSearchST.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*************************************************************************
* Compilation: javac SequentialSearchST.java
* Execution: java SequentialSearchST
* Dependencies: StdIn.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/31elementary/tinyST.txt
*
* Symbol table implementation with sequential search in an
* unordered linked list of key-value pairs.
*
* % more tinyST.txt
* S E A R C H E X A M P L E
*
* % java SequentialSearchST < tiny.txt
* L 11
* P 10
* M 9
* X 7
* H 5
* C 4
* R 3
* A 8
* E 12
* S 0
*
*************************************************************************/
public class SequentialSearchST<Key, Value> {
private int N; // number of key-value pairs
private Node first; // the linked list of key-value pairs
// a helper linked list data type
private class Node {
private Key key;
private Value val;
private Node next;
public Node(Key key, Value val, Node next) {
this.key = key;
this.val = val;
this.next = next;
}
}
// return number of key-value pairs
public int size() { return N; }
// is the symbol table empty?
public boolean isEmpty() { return size() == 0; }
// does this symbol table contain the given key?
public boolean contains(Key key) {
return get(key) != null;
}
// return the value associated with the key, or null if the key is not present
public Value get(Key key) {
for (Node x = first; x != null; x = x.next) {
if (key.equals(x.key)) return x.val;
}
return null;
}
// add a key-value pair, replacing old key-value pair if key is already present
public void put(Key key, Value val) {
if (val == null) { delete(key); return; }
for (Node x = first; x != null; x = x.next)
if (key.equals(x.key)) { x.val = val; return; }
first = new Node(key, val, first);
N++;
}
// remove key-value pair with given key (if it's in the table)
public void delete(Key key) {
first = delete(first, key);
}
// delete key in linked list beginning at Node x
// warning: function call stack too large if table is large
private Node delete(Node x, Key key) {
if (x == null) return null;
if (key.equals(x.key)) { N--; return x.next; }
x.next = delete(x.next, key);
return x;
}
// return all keys as an Iterable
public Iterable<Key> keys() {
Queue<Key> queue = new Queue<Key>();
for (Node x = first; x != null; x = x.next)
queue.enqueue(x.key);
return queue;
}
/***********************************************************************
* Test client
**********************************************************************/
public static void main(String[] args) {
SequentialSearchST<String, Integer> st = new SequentialSearchST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
for (String s : st.keys())
StdOut.println(s + " " + st.get(s));
}
}