リンクリストに対して前後に要素を追加・削除

#リンクリストの要素を追加・削除
リンクリストに対して前または裏から要素を追加したり、削除するコードは下記となります。

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

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

//function declaration
void addqatend(struct node **, struct node **, int);
void addqatbeg(struct node **, struct node **, int);
int delqatbeg(struct node **, struct node **);
int delqatend(struct node **, struct node **);
void q_display(struct node *);
int count(struct node *);

void main(){
  //declare front and rear pointer
  struct node *front, *rear;
  //declare variable to hold value of node
  int item;
  
  //set front and rear pointer to empty linked list
  front = rear = NULL;
  
  //add node to linked list
  addqatend(&front, &rear, 11);
  addqatbeg(&front, &rear, 10);
  addqatend(&front, &rear, 12);
  addqatend(&front, &rear, 13);
  addqatend(&front, &rear, 14);
  addqatend(&front, &rear, 15);
  addqatend(&front, &rear, 16);
  addqatend(&front, &rear, 17);
  addqatbeg(&front, &rear, 9);
  
  //show nodes in linked list
  q_display(front);
  printf("No. of items in linked list = %d\n", count(front));
  
  printf("Items extracted from linked list:\n");
  //get value of node
  item = delqatbeg(&front,&rear);
  printf("%d\n",item);
  
  //get value of node
  item = delqatbeg(&front,&rear);
  printf("%d\n",item);
  
  //get value of node
  item = delqatbeg(&front,&rear);
  printf("%d\n",item);
  
  //get value of node
  item = delqatend(&front,&rear);
  printf("%d\n",item);
  
  printf("After extraction\n");
  
  //show nodes in linked list
  q_display(front);
  printf("No. of items in linked list=%d\n",count(front));
  
}

//add node to the begining of linked list
void addqatbeg(struct node **f, struct node **r, int num){
  struct node *s;
  
  //create new node
  s = malloc(sizeof(struct node));
  //set value of new node
  s->data = num;
  //set link of new node to point to the node at the beginning of linked list
  s->link = *f;
  //set front pointer to new node
  *f = s;
  //if linked list is empty, set rear pointer to point to the first node
  if(*r == NULL){
    *r = s;
  }  
}

//add node to end of linked list
void addqatend(struct node **f, struct node **r, int num){
  struct node *s;
  
  //create new node
  s = malloc(sizeof(struct node));
  //set value of new node
  s->data = num;
  //set link of new node to point to null
  s->link = NULL;
  if(*r != NULL){ //if linked list is not empty
    //set link of last node to point to new node
    (*r)->link = s;
  }else{ // if linked list is empty
    //set front pointer to first node
    *f = s;
  }
  //set rear pointer to point to new node
  *r = s;
}

//get value of front node and delete it
int delqatbeg(struct node **f, struct node **r){
  int num;
  struct node *temp;
  
  //if linked list is empty, display message and return 0
  if(*f == NULL){
    printf("linked list is empty.\n");
    return 0;
  }
  
  //set locator pointer to the front of linked list
  temp = *f;
  //move front pointer to the next node
  *f = temp->link;
  //get value of front node before deleting
  num = temp->data;
  //delete node
  free(temp);
  //if linked list is empty after deletion
  if(*f == NULL){
    //set rear pointer to null
    *r == NULL;
  }
  
  return num;
}

//get value of last node and delete it
int delqatend(struct node **f, struct node **r){
  int num;
  struct node *current, *prev;
  
  //set locator pointer to front of linked list
  current = prev = *f;
  
  //if linked list is empty, print message and return 0
  if(current == NULL){
    printf("linked list is empty.\n");
    return 0;
  }
  
  //loop through linked list to find second last node
  while(current != *r){
    //keep previous node address
    prev = current;
    //go to next node
    current = current->link;
  }
  
  //get value of last node
  num = (*r)->data;
  
  //point link of second last node to point to null
  prev->link = NULL;
  
  //delete last node
  free(*r);
  
  //if linked list has only one node and be empty after deletion
  if(*f == *r){
    *r = *f = NULL;
  }else{
    //set rear pointer to latest last node
    *r = prev;
  }
  
  return num;
}

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

//count number of nodes in linked list
int count(struct node *f){
  int c = 0;
  
  while(f != NULL){
    c++;
    f = f->link;
  }
  
  return c;
}
```