Pointerで昇順リストを作成する

#昇順リストの作成
C言語で昇順リストの作成方法の勉強をやってたので、書いたコードのメモは下記となります。

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

//declare node structure
struct node{
  int data;
  struct node *link;
};

//declaration of function
void add(struct node **, int);
void display(struct node *);
int count(struct node *);
void delete(struct node **, int);

void main(){
  //create pointer to point to first node in linked list
  struct node *p;
  //set pointer to point to empty linked list
  p = NULL;

  //add nodes to linked list
  add(&p,5);
  add(&p,1);
  add(&p,6);
  add(&p,4);
  add(&p,4);
  add(&p,7);
  add(&p,10);
  add(&p,-1);

  //display linked list
  display(p);
  //count number of nodes in linked list
  printf("No. of elements in Linked List = %d\n", count(p));

  //delete nodes in linked list
  delete(&p,7);
  delete(&p,4);
  delete(&p,5);
  delete(&p,9);
  delete(&p,-1);

  //display linked list
  display(p);
  //count number of nodes in linked list
  printf("No. of elements in Linked List = %d\n", count(p));
}

//delete node in linked list
void delete(struct node **q, int num){
  //declare locator pointer
  struct node *temp,*prev;
  //set locator pointer
  temp = *q;

  //loop through linked list
  while(temp!=NULL){
    //if target is the first node
    if((temp==*q) && (temp->data == num)){
      //change p to point to next node
      *q = temp->link;
      //delete node
      free(temp);
      break;
    }
    //if target is found
    if(temp->data == num){
      //change link of previous node to point to target's next node
      prev->link = temp->link;
      //delete node
      free(temp);
      break;
    }
    //keep previous node info
    prev = temp;
    //go to next node
    temp = temp->link;
  }
  //if looped through all the linked but didn't found matching num
  if(temp==NULL){
    printf("Delete target not found\n");
  }
}

//add node in ascending order
void add(struct node **q, int num){
  //declare pointer
  struct node *r,*temp,*prev;

  //if p is pointing to NULL
  if(*q==NULL){
    //create first node
    r = malloc(sizeof(struct node));
    r->data = num;
    r->link = *q;
    //point p to new node
    *q = r;
  }else if((*q)->data > num){ // if num is smaller than data pointed by p
    //create new node
    r = malloc(sizeof(struct node));
    r->data = num;
    //set link of new node to address pointed by p
    r->link = *q;
    //set p to point to new node
    *q = r;
  }else{//if num is larger than data pointed by p
    //initialize location pointer
    temp = prev = *q;
    //loop through linked pointer
    while(temp!=NULL){
      //if num is smaller than current node but bigger than previous node
      if((temp->data >= num) && (num > prev->data)){
        //create new node
        r = malloc(sizeof(struct node));
        r->data = num;
        //set new node link to point to current
        r->link = temp;
        //set previous node link to point to new node
        prev->link = r;
        break;
      }
      //keep previous node address
      prev = temp;
      //go to next node
      temp = temp->link;
    }
    //if last node is reached
    if(temp==NULL){
      //create new node
      r = malloc(sizeof(struct node));
      r->data = num;
      //set link of new node to point to null
      r->link = temp;
      //set link of previous node to point to new node
      prev->link = r;
    }
  }
}

//display linked list
void display(struct node *q){
  while(q!=NULL){
    printf("%d\n",q->data);
    q = q->link;
  }
}

//count number of node in linked list
int count(struct node *q){
  int c = 0;

  while(q!=NULL){
    c++;
    q = q->link;
  }

  return c;
}

```