Pointerでキューを作成

#queueを作成
連結リスト勉強の続きでキュゥーの処理(First In First Out)を連結リストで挑戦してみました。

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

//node declaration
struct node{
  int data;
  struct node *link;
};

//function declaration
void addq(struct node **, struct node **, int);
int delq(struct node **, struct node **);
void q_display(struct node *);
int count(struct node *);

int main(){
  //declare front and rear pointer
  struct node *front, *rear;
  //declare variable to contain value of node
  int item;

  //set front and rear pointer to point to empty queue
  front = rear = NULL;

  //add node to queue
  addq(&front,&rear,11);
  addq(&front,&rear,12);
  addq(&front,&rear,13);
  addq(&front,&rear,14);
  addq(&front,&rear,15);
  addq(&front,&rear,16);
  addq(&front,&rear,17);

  //display nodes in queue
  q_display(front);
  //count nodes
  printf("No. of items in queue = %d\n", count(front));

  printf("Items extracted from queue:\n");
  //remove node from front and get value of node
  item = delq(&front,&rear);
  //display retrieve value
  printf("%d\n",item);
  
  //remove node from front and get value of node
  item = delq(&front,&rear);
  //display retrieve value
  printf("%d\n",item);

  //remove node from front and get value of node
  item = delq(&front,&rear);
  //display retrieve value
  printf("%d\n",item);

  printf("After extraction\n");
  
  //display nodes in queue
  q_display(front);
  //count nodes
  printf("No. of items in queue = %d\n", count(front));
}

//add nodes to queue
void addq(struct node **f, struct node **r, int num){
  struct node *n, *temp;
  
  //if queue is empty
  if(*f==NULL){
    //create new node
    n = malloc(sizeof(struct node));
    //set node value
    n->data = num;
    //set link of node to null
    n->link = NULL;
    //set front and rear pointer to point to new node
    *f = n;
    *r = n;
  }else{  //if queue is not empty
    //create new node
    n = malloc(sizeof(struct node));
    //set node value
    n->data = num;
    //set link of node to null
    n->link = NULL;
    //set last node to point to new node
    (*r)->link = n;
    //set rear pointer to point to new node
    *r = n;
  }
}

//get value of node and delete node
int delq(struct node **f, struct node **r){
  int num;
  struct node *temp;
  
  //set locator pointer to value pointed by front pointer
  temp = *f;
  
  //if queue is empty
  if(temp == NULL){
    printf("The queue is empty.\n");
    return 0;
  }
  //if queue has only one node
  else if(temp->link == NULL){
    //set front and rear pointer to null
    *f = *r = NULL;
    //get value of node
    num = temp->data;
    //delete node
    free(temp);
  }
  //if queue has multiple node
  else{
    //set front pointer to next node
    *f = temp->link;
    //get value of node
    num = temp->data;
    //delete node
    free(temp);
  }
  
  return num;
}

//count number of nodes in queue
int count(struct node *q){
  int c = 0;
  
  //loop through queue
  while(q!=NULL){
    c++;
    //go to next node
    q = q->link;
  }

  return c;
}

//display nodes in queue
void q_display(struct node *q){
  //loop through queue
  while(q!=NULL){
    printf("%d\n",q->data);
    //go to next node
    q = q->link;
  }
}

```