リンクリストをソートする(ポインター交換)

#バブルソート
下記書いた記事のバブルソートはノード同士が持っているdataをそのまま交換させました。
https://qiita.com/sabewe/items/18a8b91082c23cc77a93

今回のコードはノード同士が保持しているポインター情報を交換させます。この方法の利点としてはノードが大量のデータ変数を持っても、リンク情報を交換するだけでソートが完了します。

```sort_l.c
#include <stdio.h>
#include <stdlib.h>

//node declaration and pointer declaration
struct node{
  int data;
  struct node *link; //交換対象
} *start;

//function declaration
void getdata();
void append(struct node **, int);
int count(struct node *);
void displaylist();
void bubble_sort(int);

void main(){
  //declare value to get length of list
  int n;
  
  //create list
  getdata();
  
  //display list
  printf("Linked list before sorting: \n");
  displaylist();

  //get length of list
  n = count(start);

  //sort the list
  bubble_sort(n);
  
  //display list
  printf("Linked list after bubble sorting:\n");
  displaylist();
  
}

//bubble sort
void bubble_sort(int num){
  //declare locator pointer. current is for the current node, prev is for the previous node, t is for loop.
  struct node *current, *t, *prev;
  
  //declare pointer for address exchange
  struct node *temp;
  
  //declare variable for loop
  int i;
  
  //loop for the number of node -1
  for(i=0; i<(num-1); i++){
  
    //declare counter of swap
    int c = 0;
  
    //set locator pointers to the front of list
    t = prev = start;
    
    //loop until the second last node
    while(t->link!=NULL){
      //set locator to current node
      current = t;
        
      //if value of current node is bigger than the next node, swap the whole node
      if(current->data > (current->link)->data){
      
        //if exchange happened at the beginning  of list
        if(current == start){
          //set list pointer to new node
          start = current->link;
        }
      
        //change link of previous node
        prev->link = current->link;

        //before swapping link of each node, set locator node
        t = current;
        prev = current->link;
        
        //keep link  of next node
        temp = (current->link)->link;
        
        //exchange link  of next node
        (current->link)->link = current;
        
        //exchange link of current node
        current->link = temp;
        
        //add counter
        c++;
        
      }else{ //if no swap occurs
        //keep address of current node
        prev = t;
        
        //go to next node
        t = t->link;
      }
    }
    
    printf("Loop: %d, Swap: %d\n", i, c);
    
    //if swap doesn't occur, dont finish loop
    if(c == 0){
      break;
    }
  
  }
}

//get value for nodes to be added to list.
void getdata(){
  //declare variable to get inputed value
  int val;
  //declare variable to get yes or no answer. declare variable to clear stdin
  int ch, cs;
  //declare pointer to new list
  struct node *new;
  
  //set pointer to empty
  new = NULL;
  
  //loop for adding nodes to list
  do{
    //ask for value of node
    printf("Enter a value: ");
    scanf("%d", &val);

    //clear stdin. if this is not done, newline will be read into ch.
    while( (cs=getchar()) != '\n' && cs!= EOF);
    
    //append node to new list
    append(&new, val);
    
    //ask if want to add more nodes
    printf("Any more nodes(Y/N): ");
    //get one character
    ch = getchar();
  }while(ch == 'y' || ch == 'Y');
  
  //set pointer to new list
  start = new;
}

//append node to list
void append(struct node **q, int num){
  struct node *temp, *r;
  
  //create new node
  r = malloc(sizeof(struct node));
  //set value of new node
  r->data = num;
  //set link of new node to null
  r->link = NULL;
  
  //if list is empty
  if(*q==NULL){
    //set pointer to new node
    *q = r;
  }else{
    //set locator pointer to top of list
    temp = *q;
    
    //go to last node
    while(temp->link != NULL){
      temp = temp->link;
    }
    
    //set link of last node to new node
    temp->link = r;
    
  }
}

//display node in list
void displaylist(){
  struct node *q;
  
  //set locator pointer to list
  q = start;
  
  //loop through list
  while(q != NULL){
    //print data
    printf("%d\n",q->data);
    //go to next node
    q = q->link;
  }
}

//get length of list
int count(struct node *q){
  //declare variable to store number of nodes
  int c = 0;
  
  //loop through list
  while(q!=NULL){
    //add to count
    c++;
    //go to next node
    q = q->link;
  }
  
  return c;
}
```