title: Simulating Not Found Error In C HashTables
body: <p>I am learning data structures in my university and my choice of language is C. While I was implementing a Hash Table, I ran into somewhat of a hiccup with the C language. I am new to C, and from what I know the language does not support error throwing such as <code>throw new Exception()</code> in Java. I am trying to create a <code>get()</code> function that returns an integer value upon a successful find or indicates to the caller (without a run time error that causes the program to crash) that the value was not found. I need my table to support the full range of integer values so I cannot designate a special token value either.</p>
  
  <p>I know that I could perform a search beforehand and then call the get() function if the search returned something other than <code>NULL</code> but I was hoping for a more elegant (one function call kindof) solution to this. </p>
  
  <p>Also, I am hesitant in returning the Node of the List because I want to keep the implementation of the HashTable as abstract as possible therefore I want the interface of the Hashtable to only take in keys and return values. (I do not want to expose it's implementation)</p>
  
  <pre class="lang-c prettyprint-override"><code>
  typedef int T;
  
  typedef int Key;
  
  typedef struct node_t {
      Key key;
      T val;
      struct node_t *next;
      struct node_t *prev;
  }   Node;
  
  typedef struct {
      Node *root;
      Node *tail;
      int size;
  }   List;
  
  typedef struct {
      int size;
      List **elements;
  }   HashTable;
  
  
  Node *search(List *list, T key) {
      if (list == NULL) return NULL;
      Node *current = list-&gt;root;
      while (current != NULL &amp;&amp; current-&gt;key != key) {
          current = current-&gt;next;
      }
      return current;
  }
  
  T get(HashTable *table, Key key) {
      unsigned int index = hash(key) % table-&gt;size;
      Node *res = search(table-&gt;elements[index], key);
      if (res != NULL)
          return res-&gt;val;
      else {
          // What to do here!!!!!
      }
  }
  
  </code></pre>
  
  <p>Please assume the other functions are implemented correctly, as I am more interested in how to solve this <code>get()</code> problem. Thanks a lot for your help.</p>