title: Bubble Sort through a doubly linkrd
body: <p>am learning C with Kochans programming in C 4th edition.  I am on pointers, and my exercise is to write a pointer version of a sort function from a previous chapter.  This is a bubble sort.  I bit some code from a better coder for creating a doubly linked list (setting all the numbers as done in previous problems seemed pretty tedious).</p>
  
  <p>In this sort, I am swapping the value.  Next, I will swap the entire nodes, though im unable to get this simpler one to work.  I tested the algorithm outside of the sort function and it worked except that it did not finish the job.  So I brought some bool variables into the game, but it exited the loop when upon reaching the first "true" result, so I created a function to test whether the list was sorted or not.  It enters an infinite loop, but I do not understand why.  There are other questions like mine, but all for C++, not C.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;stdbool.h&gt;
  
  struct Node  {
      int data;
      struct Node* next;
      struct Node* prev;
  };
  
  struct Node* head; 
  
  struct Node* GetNewNode(int x) {
      struct Node* newNode
          = (struct Node*)malloc(sizeof(struct Node));
      newNode-&gt;data = x;
      newNode-&gt;prev = NULL;
      newNode-&gt;next = NULL;
      return newNode;
  }
  
  void InsertAtHead(int x) {
      struct Node* newNode = GetNewNode(x);
      if(head == NULL) {
          head = newNode;
          return;
      }
      head-&gt;prev = newNode;
      newNode-&gt;next = head;
      head = newNode;
  }
  
  void InsertatEnd(int x)
  {
  struct Node* temp = head;
  
  struct Node* newNode = GetNewNode(x);
      if(head == NULL){
          head = newNode;
          return;
      }
      while(temp-&gt;next != NULL)
      temp = temp-&gt;next;
      temp-&gt;next = newNode;
      newNode-&gt;prev = temp;
  }
  
  void Print() {
      struct Node* temp = head;
      printf("Forward: ");
      while(temp != NULL) {
          printf("%d ",temp-&gt;data);
          temp = temp-&gt;next;
      }
      printf("\n");
  }
  
  bool testSort(void)
  {
      struct Node* temp = head;
  
      bool test = false;
  
      while(temp-&gt;next != NULL)
     {
         if (temp-&gt;data &gt; temp-&gt;next-&gt;data){
              return false;
              }
      temp = temp-&gt;next;
      }
      return true;
  }
  
  void sortList(void)
  {
     struct Node* temp = head;
     int temp2;
      bool sorted = false;
  
     while(sorted == false){
      while(temp-&gt;next != NULL)
     {
         if (temp-&gt;data &gt; temp-&gt;next-&gt;data){
              temp2 = temp-&gt;next-&gt;data;
              temp-&gt;next-&gt;data = temp-&gt;data;
              temp-&gt;data = temp2;
              }
              temp = temp-&gt;next;
     }
          sorted = testSort();
  }
  }
  
  
  int main(void)
  {
      head = NULL;
      int x;
      int array[16] = {34, -5, 5, 0, 12, 100, 56, 22, 44, -3, -9, 12, 17, 22, 6, 11};
      int count;
  
      struct Node* temp;
  
      for(count = 0; count &lt; 16; ++count)
              InsertatEnd(array[count]);
  
      sortList();
      Print();
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks in advance.</p>