title: First node added to doubly linked list is missing
body: <p>Hey am trying to implement a generic list iterator that can store elements of any type. It has other files to tackle positive integer types and string types. The file compiles properly. However,when i add the nodes to the list and print it as a list where the first node entered is at the last of the list and the last node entered is at the start of the list(like prepending the list)(The code is tested in test case 2 below),it just "forgets" the first node entered.I even tried to move from the first element of the list and print one by one but it seems like the first node entered is just not there<br><br>
  This is the add function:</p>
  
  <pre><code>int  add(IteratorG it, void *vp){
  
    Node *temp;
    if ((temp = malloc(sizeof(Node))) == NULL) { 
      return 0; 
    }
  
    temp-&gt;value = it-&gt;newElm(vp);
  
  
    if(it-&gt;curr!=NULL)
    {    
  
    temp-&gt;next=it-&gt;curr;
    it-&gt;curr-&gt;prev=temp;
    it-&gt;curr=temp;
    }
  
    if(it-&gt;curr==NULL){
  
    it-&gt;curr=temp;
    it-&gt;curr-&gt;next=NULL;
    }  
  
    return 1;
  }
  </code></pre>
  
  <p>I am using a Linux environment and the errors are indicative from the results. Here is the code for the whole program:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;assert.h&gt;
  #include "iteratorG.h"
  
  typedef struct Node {
  
    void *value;  // value of thee list item
    struct Node *prev;
    // pointer previous node in list
    struct Node *next;
    // pointer to next node in list
  
    // implemented struct here .. 
  } Node;
  
  typedef struct IteratorGRep {
  
    int  numofit;      // count of items in list
    Node *head;         // first node in list
    Node *curr;       // current node in list
    Node *tail;       // last node in list
  
    ElmCompareFp  cmpElm;
    ElmNewFp  newElm;
    ElmFreeFp freeElm;
  
    // implemented struct here .. 
  
  } IteratorGRep;
  
  
  /*
  
  
    //Your  functions below .... 
   */
  
  
  IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp){
  
      IteratorG newit;
  
  
    if ((newit = malloc(sizeof (struct IteratorGRep)))==NULL)
    {
      printf("Error...! \n");
  
    }
  
    //assert (newit != NULL);
    newit-&gt;numofit = 0;
    newit-&gt;head = NULL;
    newit-&gt;tail = NULL;
    newit-&gt;curr = NULL;
    newit-&gt;cmpElm=cmpFp;
    newit-&gt;newElm=newFp;
    newit-&gt;freeElm=freeFp;
    return newit;
  
      // implemented function here and changed return value 
  }
  
  
  
  int  add(IteratorG it, void *vp){
  
    Node *temp;
    if ((temp = malloc(sizeof(Node))) == NULL) { 
      return 0; 
    }
  
    temp-&gt;value = it-&gt;newElm(vp);
    temp-&gt;next=NULL;
  
    if(it-&gt;curr==NULL)
    {
      //temp-&gt;next=it-&gt;curr;
      it-&gt;curr=temp;
      it-&gt;tail=it-&gt;head=it-&gt;curr;
    }  
  
    temp-&gt;next=it-&gt;curr;
    it-&gt;curr-&gt;prev=temp;
    it-&gt;curr=temp;
    it-&gt;head=it-&gt;curr;
    //it-&gt;curr-&gt;prev=temp;
    /*temp-&gt;next = it-&gt;curr;
    it-&gt;curr = temp;
    */
      // add node to list from front 
      return 1;
  }
  int  hasNext(IteratorG it){
  
    if(it-&gt;curr-&gt;next==NULL)
     {
       return 0;
     }
  
  
      // check if theres next element/node
      return 1;
  }
  int  hasPrevious(IteratorG it){
  
      if(it-&gt;curr-&gt;prev!=NULL)
      {
        return 1;
      }
      // check if theres previous element/node 
      return 0;
  }
  void *next(IteratorG it){
  
    Node *tempo;
  
    if(it-&gt;curr-&gt;next==NULL)
    {
      return NULL;
    }
    tempo=it-&gt;curr;
    it-&gt;curr=it-&gt;curr-&gt;next;
  
    // implemented function here  
    return tempo-&gt;value;
  }
  void *previous(IteratorG it){
  
      Node *tempor;
  
    tempor=it-&gt;curr;
    if(tempor-&gt;prev==NULL)
    {
      return NULL;
    }
    tempor=it-&gt;curr-&gt;prev;
  
    it-&gt;curr=it-&gt;curr-&gt;prev;
    //tempor=it-&gt;curr;
  
    // move to next node in list  
    return tempor-&gt;value;
  }
  int  del(IteratorG it){
    if(it-&gt;curr-&gt;prev!=NULL)
    {
      Node *temp_curr=it-&gt;curr;
      Node *temp_prev=it-&gt;curr-&gt;prev-&gt;prev;
      temp_curr-&gt;prev=temp_prev;
      temp_prev-&gt;next=temp_curr;
      return 1;
  
    }// delete previous node  from list 
    else
      return 0;
  }
  int  set(IteratorG it, void *vp){
    if(it-&gt;curr-&gt;prev!=NULL)
    {
  
    it-&gt;curr-&gt;prev-&gt;value=vp;
  
  
    /*  
  
    Node *wep;
    if ((wep = malloc(sizeof(Node))) == NULL) { 
        return 0; 
    }  
  
  
    wep-&gt;value=it-&gt;newElm(vp);  
  
  
    store_next=it-&gt;curr;
    store_prev=it-&gt;curr-&gt;prev-&gt;prev;
    store_next-&gt;prev=wep;
     wep-&gt;next=store_next;
  
    store_prev-&gt;next=wep;
    wep-&gt;prev=store_prev;
    */
  
  
    return 1;
    }
      // change previous node value with new 
      return 0;
  }
  IteratorG advance(IteratorG it, int n){
  
  
  
      //To be implemented
      //move forward by n times
      return NULL;
  }
  void reverse(IteratorG it){
    Node *curr = it-&gt;head;
    Node *temp = NULL;
    while(curr != NULL) {
      temp = curr-&gt;next;
      curr-&gt;next = curr-&gt;prev;
      curr-&gt;prev = temp;
      curr = temp;
    }
    temp = it-&gt;head;
    it-&gt;head = it-&gt;tail;
    it-&gt;tail = temp;// reverse elements of whole list
  }
  IteratorG find(IteratorG it, int (*fp) (void *vp) ){
      // To be implemented 
      // Find elements of vp in list after current position and put in new list.return the list.
        return NULL;
   }
  int distanceFromStart(IteratorG it){
  
    Node *c=it-&gt;curr;
    int count=0;
  
  
    while(c-&gt;prev!=NULL)
    {
      c=c-&gt;prev;
      count++;
    }
    return count;
      // count number of elements from start of list to current position 
  
  }
  int distanceToEnd(IteratorG it){
  
    Node *cu=it-&gt;curr;
    int count=0;
  
    while(cu-&gt;next!=NULL)
    {
      cu=cu-&gt;next;
      count++;
    }
    return count;
      // count number of elements from end of list to current position
  }
  void reset(IteratorG it){
  
  
  
    while(it-&gt;curr-&gt;prev!=NULL)
    {
  
      it-&gt;curr=it-&gt;curr-&gt;prev;
  
    }
    return;
      // move current position to start of list
  
  }
   void freeIt(IteratorG it){
    assert(it != NULL);
    Node *curr, *prev;
    curr = it-&gt;head;
    while (curr != NULL) {
      prev = curr;
      curr = curr-&gt;next;
      free(prev-&gt;value);
      free(prev);
    }
    free(it); // free items  
  
  }
  </code></pre>
  
  <p>This is the header file for the code:</p>
  
  <pre><code>#ifndef LISTITERATORG_H
  #define LISTITERATORG_H
  
  #include &lt;stdio.h&gt;
  
  typedef struct IteratorGRep *IteratorG;
  
  typedef int   (*ElmCompareFp)(void const *e1, void const *e2);
  typedef void *(*ElmNewFp)(void const *e1);
  typedef void  (*ElmFreeFp)(void *e1);
  
  
  IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp);
  int  add(IteratorG it, void *vp);
  int  hasNext(IteratorG it);
  int  hasPrevious(IteratorG it);
  void *next(IteratorG it);
  void *previous(IteratorG it);
  int  del(IteratorG it);
  int  set(IteratorG it, void *vp);
  IteratorG advance(IteratorG it, int n);
  void reverse(IteratorG it);
  IteratorG find(IteratorG it, int (*fp) (void *vp) );
  int distanceFromStart(IteratorG it);
  int distanceToEnd(IteratorG it);
  void reset(IteratorG it);
  void freeIt(IteratorG it);
  
  #endif
  </code></pre>
  
  <p>One of the functions have yet to be implemented and is indicated in the code itself. But I guess that might not be the source of issue here. I perceive that the add function is not being utilized properly here.</p>
  
  <p>EDIT:
  heres the test case code. Theres no errors in the test case code just in the program above only :</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;string.h&gt;
  #include "iteratorG.h"
  #include "positiveIntType.h"
  #include "stringType.h" 
  
  #define MAXARRAY 5
  
  /* Helper Functions Below */
  
  /* Returns 1 if marks &gt;= 50, 0 otherwise  */
  int passMarks(void *marks){
    return (*((int *) marks) &gt;= 50); 
  
    /* Easy to understand below .. 
       int *ip = (int *) marks;
       if(*ip &gt;= 50) { return 1; }
       else { return 0; } 
    */
  }
  
  /* Returns 1 if str starts with "jo" */
  int prefixJo(void *str){
    return (strncmp("jo", (char *) str, 2) == 0) ; 
  }
  
  /* A function to print a string from a void pointer */
  void prnStr(void *vp){
    assert(vp != NULL);
    printf(" %s", (char *) vp );      
  }
  
  /* A function to print an integer from a void pointer */
  void prnInt(void *vp){
    assert(vp != NULL);
    printf(" %d", *((int *) vp) );    
  }
  
  /* Prints previous element using the given function 'fp'
     examples: prnPrev(it1, prnInt); prnPrev(it2, prnStr);
  */
  void prnPrev(IteratorG it, void (*fp) (void *p) ){
    void *prevP = previous(it);
    assert(prevP != NULL);
    printf("&gt; Previous value is: "); 
    fp(prevP);
    printf("\n"); 
  }
  
  /* Prints next element using the given function 'fp'
     examples:   prnNext(it1, prnInt); prnNext(it2, prnStr);
  */
  void prnNext(IteratorG it, void (*fp) (void *p) ){
    void *nextP = next(it);
    assert(nextP != NULL);
    printf("&gt; Next value is: "); 
    fp(nextP);
    printf("\n"); 
  }
  
  /* Prints elements of 'it' from current to last position 
     using the given function 'fp'. The current position 
     of 'it' will change to the end of the list.
     examples: prnIt(it1, prnInt); prnIt(it2, prnStr);
  */
  void prnIt(IteratorG it, void (*fp) (void *p) ){
    int count = 0;
    while(hasNext(it)){
      void *nextP = next(it); 
      count++;
      if(count &gt; 1) { printf(", "); }
      fp(nextP);      
    }
    printf("\n"); 
  }
  
  
  /* Few Tests Below  */
  
  void test1(){
    printf("\n--====  Test-01       ====------\n");
    IteratorG it1 = newIterator(positiveIntCompare, positiveIntNew,  positiveIntFree);
    int a[MAXARRAY] = { 25, 78, 6, 82 , 11};
    for(int i=0; i&lt;MAXARRAY; i++){
      int result = add(it1 , &amp;a[i]); 
      printf("&gt; Inserting %d: %s \n", a[i], (result==1 ? "Success" : "Failed") );
    }
    freeIt(it1);
    printf("--====  End of Test-01 ====------\n");
  }
  
  void test2(){
    printf("\n--====  Test-02       ====------\n");
    IteratorG it1 = newIterator(positiveIntCompare, positiveIntNew, positiveIntFree);
    int a[MAXARRAY] = { 72, 14, 62, 8, 93};
    for(int i=0; i&lt;MAXARRAY; i++){
      int result = add(it1 , &amp;a[i]); 
      printf("&gt; Inserting %d: %s \n", a[i], (result==1 ? "Success" : "Failed") );
    }
  
    prnNext(it1, prnInt);
    prnNext(it1, prnInt);
    prnPrev(it1, prnInt);
  
    int newVal1 = 55;
    int result1 = set(it1, &amp;newVal1);
    printf("&gt; Set value: %d ; return val: %d \n", newVal1,  result1 );  
  
    prnPrev(it1, prnInt);
  
    freeIt(it1);
    printf("--====  End of Test-02 ====------\n");
  }
  
  
  
  int main(int argc, char *argv[])
  {
  
  
    test1();
    test2();
  
    return EXIT_SUCCESS;
  
  }
  </code></pre>