title: unknown deletion of node before deletion doubly Linked linked list
body: <p>After my first iteration of addTrash and then to the first iteration of modifyMainList it segfaults because of a null value in the location of the index that i was just going to take out of the main list via modifyMainList. addTrash should not be deleting anything at all, so i think it must be a pointer issue, but i dont know for sure. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  
  struct node {
      int value;
      struct node *next, *previous;
  };
  
  struct node * modifyMainList(struct node *mainHead, int link2Delete){
  
      printf("inside modify list\n\n");
      struct node *curr, *temp;
      temp = NULL;
      curr = mainHead;
      int i;
  
      for (i = 0; i &lt; link2Delete; i++){
  
          printf("%d\n", i);
          curr = curr -&gt; next;
      }
  
      if(curr -&gt; previous == NULL){
  
          temp = curr;
          curr = curr -&gt; next;
  
          curr -&gt; previous = NULL;
  
          temp -&gt; next = NULL;
          free(temp);
  
          return mainHead;
      }else{
  
          if((curr -&gt; next == NULL) &amp;&amp; (curr -&gt; previous != NULL)){
  
              temp = curr;
              curr = curr -&gt; previous;
              curr -&gt; next = NULL;
  
              temp -&gt; previous = NULL;
              free(temp);
  
              return mainHead;
          }else{
  
              temp = curr;
              curr = curr -&gt; previous;
              curr -&gt; next = curr -&gt; next -&gt; next;
              curr = temp -&gt; next;
              curr -&gt; previous = curr -&gt; previous -&gt; previous;
  
              temp -&gt; previous = NULL;
              temp -&gt; next = NULL;
              free(temp);
  
              return mainHead;            
          }
      }
  }
  
  struct node * addTrash(struct node *trashHead, struct node *mainHead, int link2Delete){
  
      struct node *curr = mainHead, *trashCurr = NULL, *temp = NULL;
      int i = 0;
  
      for(i = 0; i &lt; link2Delete; i++){
  
          curr = curr -&gt; next;
      }
  
      printf("inside addTrash\n\n");
      if(trashHead == NULL){
  
          trashHead = curr;
          trashHead -&gt; previous = NULL;
          trashHead -&gt; next = NULL;
  
  
          return trashHead;
  
      }else{
  
          trashCurr = trashHead;
          while(trashCurr -&gt; next != NULL){
  
              trashCurr = trashCurr -&gt; next;
          }
  
          trashCurr -&gt; next = curr;
          temp = curr;
          temp -&gt; previous = trashCurr;
          temp -&gt; next = NULL;
  
          temp = NULL;
          free(temp);
  
          trashCurr = NULL;
          free(trashCurr);
  
          return trashHead;
  
      }
  }
  
  //Traverses and prints out data from left to right
  void TraverseLeftRight(struct node *head){
  
      struct node *current;
      current = head;
  
      while(1){
          if(current != NULL){
  
              printf("Left to right output:           %d\n", current -&gt; value);
              current = current -&gt; next;
          }else{
              break;
          }           
      }
  }
  
  
  //Traverses and prints out data from right to left
  void TraverseRightLeft(struct node *tail){
  
      struct node *current;
  
  
      current = tail;
  
      while(1){
          if(current != NULL){
  
              printf("Right to left output:           %d\n", current -&gt; value);
              current = current -&gt; previous;
          }else{
              break;
          }           
      }
  }
  
  
  //inserts a node into the doubly linked linked-list
  struct node * insertIntoList(struct node *head, int value){
  
      int i;
      struct node *current, *temp;
  
      for(i = 0; i &lt; value; i++){
  
          //Case 1: List empty
          if (i == 0){
  
              //create node and assign all pointers and values
              temp = (struct node *) malloc(sizeof(struct node));
              temp -&gt; value = i;
              temp -&gt; next = NULL;
              temp -&gt; previous = NULL;
              head = temp;
              current = head; 
              printf("Input data:             %d\n", current -&gt; value);
  
  
          }else{
  
              //create node and assign pointers and values
              temp = (struct node *) malloc(sizeof(struct node));
              temp -&gt; value = i;
              temp -&gt; next = NULL;
  
              //assign pointer of previous for temp to the current node
              temp -&gt; previous = current;
  
  
  
              //change current node to the node that was just created
              current -&gt; next = temp;
              current = current -&gt; next;  
              printf("Input data:             %d\n", current -&gt; value);
  
          }
      }
      printf("\n");
      return head;
  
  }
  
  //frees the data on the doubly linked linked-list
  
  void Free(struct node *head){
  
      struct node *current, *temp;
  
      current = head;
      temp = head;    
  
      while(1){
          if(current != NULL){
  
              current = current -&gt; next;
              temp -&gt; next = NULL;
              temp -&gt; previous = NULL;
              temp -&gt; value = 0;
              free(temp);
              temp = current;
  
          }else{
              break;
          }           
      }
  
  
  }
  
  int main(int argv, char **argc){
  
      struct node *head, *current, *tail, *temp, *trashHead;
      int input, link2Delete = 0, size = 0, y = 0, number2Delete = 0, i;
      head = NULL;
      trashHead = NULL;
      temp = NULL;
      current = NULL;
      tail = NULL;
  
      //Check to see if there is the correct amount of arguments
      if(argv &lt; 2){
            printf("************************************************\n");
            printf("* You must include a number for size of list.  *\n");
            printf("************************************************\n");
  
          //exit program
          return 0;
  
       }else{
          if(argv &gt; 2){
              printf("*****************************************************************\n");
              printf("* You have entered too many arguments, arguments need to be 2.  *\n");
              printf("*****************************************************************\n");
  
              //exit program
              return 0;
  
          }else{
  
              if(argv == 2){
  
                  //convert string to int
                  input = atoi(argc[1]);
  
                  //create the doubly linked linked-list
                  head = insertIntoList(head, input); 
  
  
  
                  //traverse and print values from left to right order
                  TraverseLeftRight(head);
  
                  //traverses the list to create the tail
                  current = head;
  
                  while(1){
                      if(current != NULL){
                          temp = current;
                          current = current -&gt; next;
  
                      }else{
                          break;
                      }           
                  }   
  
                  tail = temp;
  
                  printf("\n");               
  
                  //traverse and print values from right to left order
                  TraverseRightLeft(tail);
  
                  //Generate the random numbers for the corresponding names to be deleted and the numbers of
                  //deletions made
                  srand( time(NULL) );
                  size = input;
                  number2Delete = rand() % size + 1;
  
                  printf("\n\nThis is the random number: %d\n", rand());
                  printf("This is the nuber of nodes to be deleted: %d\n", number2Delete);
  
                  for(i = 0; i &lt; number2Delete; i++){
  
                      y = 0;
                      //Pick a random node for deletion
                      link2Delete = (rand() % size);
                      current = head;
                      while(current != NULL){                 
  
                          current = current -&gt; next;
                          if(current != NULL){
                              printf("this is node: %d\n", y);
                              y++;
                          }
                      }
  
                      printf("this is the node to be deleted: %d\n\n", link2Delete);
                      size--;
  
                      trashHead = addTrash(trashHead, head, link2Delete);
                      printf("this is the head of trash: %d\n\n", trashHead -&gt; value);
                      head = modifyMainList(head, link2Delete);           
                  }
  
                  Free(head);
                  return 0;
              }
          }
      }
  }
  </code></pre>