title: Split linked list into two other lists, one with odd indices the other one with even indices
body: <p>The question asked to write a function split() that copies the contents of a linked list into two other linked lists. The function copies nodes with even indices (0,2,etc)to EvenList and nodes with odd indices to oddList. The original linked list should not be modified. Assume that evenlist and oddlist will be passed into the function as empty lists  (*ptrEvenList = *ptrOddList = NULL).  </p>
  
  <p>In my program it can show the initial list. While there is something wrong with the other two list. And it causes the terminating of the program.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  #define SIZE 9
  </code></pre>
  
  <p>// define the structure of of the list of nodes</p>
  
  <pre><code>typedef struct node{
      int item;
      struct node *next;
  }ListNode;
  </code></pre>
  
  <p>//call functions</p>
  
  <pre><code>int search(ListNode *head,int value);
  void printNode(ListNode *head);
  void split(ListNode *head,ListNode **OddList,ListNode **EvenList);
  </code></pre>
  
  <p>//main function</p>
  
  <pre><code>int main(){
      ListNode *head=NULL;
      ListNode *temp;
      ListNode *OddList=NULL;
      ListNode *EvenList=NULL;
  </code></pre>
  
  <p>//in the question, it asked me to pass two empty lists to the function 'slipt'</p>
  
  <pre><code>    int ar[SIZE]={1,3,5,2,4,6,19,16,7};
      int value=0;
      int i;
  
      head=(struct node*)malloc(sizeof(ListNode));
      temp=head;
      for(i=0;i&lt;SIZE;i++){
          temp-&gt;item=ar[i];
          if(i==(SIZE-1))  //last item
              break;
          temp-&gt;next=(struct node*)malloc(sizeof(ListNode));
          temp=temp-&gt;next;
      }
      temp-&gt;next=NULL;
      printf("Current list:");
      printNode(head);
  
      split(head,&amp;OddList,&amp;EvenList);
  
  
  return 0;
  }
  </code></pre>
  
  <p><em><strong></em>****</strong>!!!!!!!!!
  the problem I think is in this part.</p>
  
  <pre><code>void split(ListNode *head,ListNode **ptrOddList,ListNode **ptrEvenList){
      int remainder;
      ListNode *tempO,*tempE,*temp;
  
  
  
      if (head==NULL)
          return;
      else{
          temp=head;
  
          *ptrOddList=(struct node*)malloc(sizeof(ListNode));
          *ptrEvenList=(struct node*)malloc(sizeof(ListNode));
  
          tempO=*ptrOddList;
          tempE=*ptrEvenList;
  
          while(temp!=NULL){
              remainder=temp-&gt;item%2;
  
              if(remainder==0){
                  tempE-&gt;next=(struct node*)malloc(sizeof(ListNode));
                  tempE-&gt;item=temp-&gt;item;
                  tempE=tempE-&gt;next;
              }
              else{
                  tempO-&gt;next=(struct node*)malloc(sizeof(ListNode));
                  tempO-&gt;item=temp-&gt;item;
                  tempO=tempO-&gt;next;
              }
              temp=temp-&gt;next;
          }
          tempE=NULL;
          tempO=NULL;
  </code></pre>
  
  <p>// I also tried <code>tempE-&gt;next=NULL;</code> and <code>tempO-&gt;next=NULL</code>
  //the program can run if I modify it like above, but the last two numbers shown will be two random numbers.</p>
  
  <pre><code>        printf("Even List:");
          printNode((*ptrEvenList));
          printf("Odd List:");
          printNode((*ptrOddList));
      }
  }
  </code></pre>
  
  <hr>
  
  <p>//function used to print out the results</p>
  
  <pre><code>void printNode(ListNode *head){
      if (head==NULL)
          return;
      while(head!=NULL){
          printf("%d ",head-&gt;item);
          head=head-&gt;next;
      }
      printf("\n");
  }
  </code></pre>