Jasewick origin
Basic here is one linked list. Following methods are implemented
first
contains
size
izEmpty
get
put
delete
iterator (IEnumerable)
Example illustrates some kind of dictionary with Key and Value, 2 examples – one just shows some standart abilities, another – counts frequency of words
a – is array of words, in source author load some book to that structure and shows that it is not so effective to use – long time operations
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /* this example illustrates symbol table realized on linked list source http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/SequentialSearchST.java.html */ namespace SequentialSearchSTExample_fromSite { class Program { static void Main(string[] args) { //FrequencyCounter_TestClient tc = new FrequencyCounter_TestClient(); //tc.Execute(); Standart_TestClient stc = new Standart_TestClient(); stc.Execute(); // foreach (var val in st) Console.WriteLine(val); //// testing iterator Console.Read(); } } public class Standart_TestClient { string[] a = { "S", "E", "A", "R", "C", "H", "E", "X", "A", "M", "P", "L", "E" }; public void Execute() { SequentialSearchST<string, int> st = new SequentialSearchST<string, int>(); //adding for (int i = 0; i < a.Length; i++) { st.put(a[i], i); } if (st.contains("X")) Console.WriteLine("Yes! COntains X"); st.ConsoleDisplay(); st.delete("L"); Console.WriteLine(); st.ConsoleDisplay(); } } public class FrequencyCounter_TestClient { string[] a = { "S", "E", "A", "R", "C", "H", "E", "X", "A", "M", "P", "L", "E" }; public void Execute() { SequentialSearchST<string, int> st = new SequentialSearchST<string, int>(); //adding for (int i = 0; i < a.Length; i++) { // to Key we write Word, to Value we write Frequency if (!st.contains(a[i])) st.put(a[i], 1); else st.put(a[i], st.get(a[i]) + 1); } // search max frequent word string maxFrequentWord = " "; st.put(maxFrequentWord, 0); foreach (var val in st) { if (st.get(val) > st.get(maxFrequentWord)) maxFrequentWord = val; } Console.WriteLine("maxFrequentWord=" + maxFrequentWord + " maxFrequency " + st.get(maxFrequentWord)); st.ConsoleDisplay(); } } public class SequentialSearchST<Key, Value> : IEnumerable<Key> { 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 { public Key key { get; set; } public Value val { get; set; } public Node next { get; set; } 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. * * @return the number of key-value pairs in this symbol table */ public int size() { return n; } /** * Returns true if this symbol table is empty. * * @return {@code true} if this symbol table is empty; * {@code false} otherwise */ public bool isEmpty() { return size() == 0; } /** * Returns true if this symbol table contains the specified key. * * @param key the key * @return {@code true} if this symbol table contains {@code key}; * {@code false} otherwise * @throws IllegalArgumentException if {@code key} is {@code null} */ public bool contains(Key key) { if (key == null) throw new ArgumentException("argument to contains() is null"); return get(key) != null; } /** * Returns the value associated with the given key in this symbol table. * * @param key the key * @return the value associated with the given key if the key is in the symbol table * and {@code null} if the key is not in the symbol table * @throws IllegalArgumentException if {@code key} is {@code null} */ public Value get(Key key) { if (key == null) throw new ArgumentException("argument to get() is null"); for (Node x = first; x != null; x = x.next) { if (key.Equals(x.key)) return x.val; } return default(Value); } /** * Inserts the specified key-value pair 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 {@code null}. * * @param key the key * @param val the value * @throws IllegalArgumentException if {@code key} is {@code null} */ public void put(Key key, Value val) { if (key == null) throw new ArgumentException("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). * * @param key the key * @throws IllegalArgumentException if {@code key} is {@code null} */ public void delete(Key key) { if (key == null) throw new ArgumentException("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 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); // recursion return x; } // iteraror System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator<Key> GetEnumerator() { var node = first; while (node != null) { yield return node.key; node = node.next; } } // public void ConsoleDisplay() { Console.WriteLine(); Console.WriteLine("key" + " " + "val"); for (Node n = first; n != null; n = n.next) { Console.WriteLine(" " + n.key + " " + n.val); } } } } |
My attempt to realize
Jasewick, p 344
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SequentialSearchSTExample_OnLinkedList { class Program { static void Main(string[] args) { string[] a = { "S", "E", "A", "R", "C", "H", "E", "X", "A", "M", "P", "L", "E" }; SequentialSearchST<string, int> st = new SequentialSearchST<string, int>(); //adding for (int i = 0; i < a.Length; i++) { st.put(a[i], i); } st.ConsoleDisplay(); st.Delete("L"); //st.ConsoleDisplay(); // testing iterator Console.WriteLine(); foreach (var val in st) Console.WriteLine(val); Console.Read(); } } public class SequentialSearchST<Key, Value> : IEnumerable<Key> { private Node first; private class Node { public Key key; public Value val; public Node next; public Node(Key key, Value val, Node next) { this.key = key; this.val = val; this.next = next; } } public Value get(Key key) { for (Node x = first; x != null; x = x.next) { if (key.Equals(x.key)) return x.val; } return default(Value);// null } public void put(Key key, Value val) { for (Node x = first; x != null; x = x.next) { if (key.Equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); // adding to the start of list } public void ConsoleDisplay() { Console.WriteLine(); Console.WriteLine("key" + " " + "val"); for (Node n = first; n != null; n = n.next) { Console.WriteLine(" " + n.key + " " + n.val); } } public int size() { int i = 0; for (Node n = first; n != null; n = n.next) { i++; } return i; } public void Delete(Key key) { Node beforeNode = null; // node before for (Node n = first; n != null; n = n.next) { // if first node if ((key.Equals(n.key)) && (n == first)) { //delete first node first = n.next; n = n.next; return; } // if last node if ((key.Equals(n.key)) && (n.next == null)) { beforeNode.next = null; return; } // if some middle node if ((key.Equals(n.key)) && (n.next != null)) { beforeNode.next = n.next; return; } beforeNode = n; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator<Key> GetEnumerator() { var node = first; while (node != null) { yield return node.key; node = node.next; } } } } |