-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathSequentialSearchST.java
More file actions
140 lines (125 loc) · 4.16 KB
/
SequentialSearchST.java
File metadata and controls
140 lines (125 loc) · 4.16 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Symbol table implementation with sequential search in an unordered linked
* list of key-value pairs.
* The `SequentialSearchST` class represents an (unordered) symbol table of
* generic key-value pairs.
* It supports the usual `put`, `get`, `contains`, `delete`, `size`, and `is-empty`
* methods.
* It also provides a `keys` method for iterating over all of the keys.
* A symbol table implements the `associative array` abstraction: when associating
* a value with a key that is already in the symbol table, the convention is to
* replace the old value with the new value.
* The class also uses the convention that values can't be `null`. Setting the value
* associated with a key to `null` is equivalent to deleting the key from the symbol
* table.
*
* This implementation uses a singly-linked list and sequential search.
* It relies on the `equals()` method to test whether two keys are equal. It does not
* call either the `compareTo` or `hashCode()` method.
* The `put` and `delete` operations take linear time; The `get` and `contains` operation
* takes linear time in the worst case. The `size` and `is-empty` operation take constant
* time. Construction takes constant time.
*
*/
public class SequentialSearchST<Key, Value> {
private int n; // number of key-value pairs
private Node first; // the linked list of key-value pairs
// 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;
}
}
// Initializes an empty symbol table
public SequentialSearchST() {
//
}
// Returns the number of key-value pairs in this symbol table.
public int size() {
return n;
}
// Returns true if this symbol table is empty.
public boolean isEmpty() {
return size() == 0;
}
// Returns true if this symbol table contains the specified key.
public boolean contains(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to contains() is null");
return get(key) != null;
}
// Returns the value associated with the given key in this symbol table.
public Value get(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to get() is null");
for(Node x = first; x != null; x = x.next) {
if(key.equals(x.key))
return x.val;
}
return null;
}
// Inserts the specified key-value pairs into the symbol table, overwriting
// the old value with the new value if the symbol table already contains the
// specified key. Deletes the specified key(and its associated value) from this
// symbol table if the specified value is `null`
public void put(Key key, Value val) {
if(key == null)
throw new IllegalArgumentException("first argument to put() is null");
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++;
}
// Removes the specified key and its associated value from this symbol table(if
// the key is in this symbol table).
public void delete(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to delete() is null");
first = delete(first, key);
}
// delete key in linked list beginning at Node x
// warning: function call stack too large if table is large
private Noed 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;
}
// Returns all keys in the symbol table as an `Iterable`.
// To iterate over all of the keys in the symbol table named `st`
// use the foreach notation: `for(Key key: st.keys())`.
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
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));
}
}
}