title: Sorted Insertion into Linked List
body: <p>AoA,</p>
  
  <p>I've been attempting to debug a problem in my circular linked list for 12hrs now. The function takes in an ADT which has a start and cursor field. The initial dummy cell points to itself. Insert elements. Repeat elements are not allowed. </p>
  
  <pre><code>    int setInsertElementSorted(setADT buffer, setElementT E)
      {
          bool isUnique = true;
          cellT *previous;
  
          previous = buffer-&gt;start;
          buffer-&gt;cursor = buffer-&gt;start-&gt;next;
  
          while(buffer-&gt;cursor != buffer-&gt;start){
              if(buffer-&gt;cursor-&gt;value == E){
                  isUnique = false;
              } else if(E &lt; buffer-&gt;cursor-&gt;value)
                  break;
                else {   
                  previous = buffer-&gt;cursor;
                  buffer-&gt;cursor = buffer-&gt;cursor-&gt;next;
              }
          }
  
          if(isUnique != false){
              cellT *newNode = malloc(sizeof(cellT));
              newNode-&gt;value = E;
              previous-&gt;next = newNode;
              newNode-&gt;next = buffer-&gt;cursor;
  
              buffer-&gt;count++;
              return (buffer-&gt;count);   
              }
      }
  </code></pre>
  
  <p>The code takes in a series of integers and then sorts them into the LL parameter. Supposed to be used for a set (hence why no repeat entries).</p>
  
  <p>The output for: 9, 8, 7, 6, 5, 4, 3, 2, 1</p>
  
  <p>is.. 3, 4, 5, 6, 7, 8, 9  (what happened to the first two values?)</p>
  
  <p>When inputting something like: 7, 3, 5, 1, 9, 2</p>
  
  <p>out is only 7, 9 (so it can't handle values separated by more than one.. o.O)</p>
  
  <p><strong>Additional info:</strong></p>
  
  <pre><code>typedef struct cellT {
      int value;
      struct cellT *next;
  } cellT;
  
  struct setCDT{
      int count;
      cellT *start;
      cellT *cursor;   
  };
  
  setADT setNew()
  {
      setADT newNode = malloc(sizeof(struct setCDT));
      newNode-&gt;start = newNode-&gt;cursor = malloc(sizeof(cellT));
      newNode-&gt;start-&gt;next = newNode-&gt;cursor-&gt;next = newNode-&gt;start;
      newNode-&gt;count = 0;
      return (newNode);
  }
  </code></pre>
  
  <p>setADT is a pointer type to setCDT. setElementT, however, is just a plain and simple <strong>int</strong>. Sorry for the ambiguity. </p>