title: Swap nodes in a linked list without swapping data
body: <p>how can i use pointers without copying the data?
  I want to write a bubble sorting function, but I got stuck, and need some help how to swap node addresses instead of values. 
  I have a file with city names, and temperatures:</p>
  
  <ul>
  <li>Las Vegas, 25</li>
  <li>New York, 33</li>
  <li>Chicago, 23</li>
  <li>Houston, 39</li>
  </ul>
  
  <p>and I need to sort it by the temperature, and write it to another file.</p>
  
  <p>UPDATE:
  Ok, now I think i understand the theoratical part:</p>
  
  <pre><code>// p is my node
  // p-prev -&gt; p -&gt; p-next -&gt; p-next-next
  prev-&gt;next = p-&gt;next;
  p-&gt;next = p-&gt;next-&gt;next;
  prev-&gt;next-&gt;next = p;
  </code></pre>
  
  <p>This is what i need to do, to swap the nodes, but syntactically i couldnt make it work.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct node {
      char name[128];
      int id;
      struct node *next;
  }*head;
  
  void readFile() {
      char fnamer[128] = "";
      printf("\nEnter the file name to read (delimiter: ,): \n");
      scanf("%s",&amp;fnamer);
      FILE *inf = fopen(fnamer, "r");
  
      char buffer[1024];
      memset(buffer, 0, 1024);
      while(fgets(buffer, 1024, inf)){
          struct node *temp = malloc(sizeof(struct node));
          temp-&gt;next = NULL;
  
          if(sscanf(buffer, "%19[^,], %d", temp-&gt;name, &amp;temp-&gt;id) != 2){
              free(temp);
              break;
          }
  
          if(!head){
              head = temp;
          } else{
              temp-&gt;next = head;
              head = temp;
          }
      }
  
      fclose(inf);
  }
  
  int main(void) {
      // Read a linked list from file
      readFile();
  
      //Bubble sort in linked list
      struct node *loop1 = head;
      while(loop1){
          struct node *loop2 = loop1-&gt;next;
          while(loop2){
              if(loop1-&gt;id &gt; loop2-&gt;id){
  
                  // Swap next pointers
                  // This is not working
                  struct node *temp = loop1-&gt;next;
                  loop1-&gt;next = loop2-&gt;next;
                  loop2-&gt;next  = temp;
  
              }
              loop2 = loop2-&gt;next;
          }
          loop1 = loop1-&gt;next;
      }
  
      // Print the sorted linked list to file:
      char foutname[100]="";
      printf("\nPlease Enter the file name to write the result: \n");
      scanf("%s",&amp;foutname);
  
      FILE *outf;
      outf = fopen(foutname, "w+");
  
      loop1 = head;
      while(loop1){
          printf("%s %d\n", loop1-&gt;name, loop1-&gt;id);
          fprintf(outf, "%s %d\n", loop1-&gt;name, loop1-&gt;id);
          loop1 = loop1-&gt;next;
      }
      fclose(outf);
      return 0;
  }
  </code></pre>