001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Exercise 3.1.5 (Solution published at http://algs4.cs.princeton.edu/)
package algs31;
import stdlib.*;
import algs13.Queue;
/* ***********************************************************************
 *  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<K, V> {
  private int N;           // number of key-value pairs
  private Node<K,V> first;      // the linked list of key-value pairs

  // a helper linked list data type
  private static class Node<K,V> {
    public final K key;
    public V val;
    public Node<K,V> next;

    public Node(K key, V val, Node<K,V> 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(K key) {
    return get(key) != null;
  }

  // return the value associated with the key, or null if the key is not present
  public V get(K key) {
    for (Node<K,V> x = first; x != null; x = x.next) {
      if (key.equals(x.key)) return x.val;
    }
    return null;
  }
  public V getR(K key) {
    return getR(key, first);
  }
  private V getR(K key, Node<K,V> x) {
    if (x == null) return null;
    if (key.equals (x.key)) return x.val;
    return getR(key, x.next);
  }

  // add a key-value pair, replacing old key-value pair if key is already present
  public void put(K key, V val) {
    if (val == null) { delete(key); return; }
    for (Node<K,V> 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(K key) {
    first = delete(first, key);
  }

  // delete key in linked list beginning at Node<K,V> x
  // warning: function call stack too large if table is large
  private Node<K,V> delete(Node<K,V> x, K 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<K> keys()  {
    Queue<K> queue = new Queue<>();
    for (Node<K,V> x = first; x != null; x = x.next)
      queue.enqueue(x.key);
    return queue;
  }




  /* *********************************************************************
   * Test client
   **********************************************************************/
  public static void main(String[] args) {
    StdIn.fromString ("S E A R C H E X A M P L E");
    SequentialSearchST<String, Integer> st = new SequentialSearchST<>();
    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));
  }
}