連結リスト(Linked List)

簡単な例(Simple Example)

#include <stdio.h>
#include <stdlib.h>

/* セルの定義 */
typedef struct cell{
    struct cell *next;
    int data;
}cell;

cell c1, c2, c3;

int main(void){

    cell *p;

    c1.next = &c2;
    c1.data = 23;
    c2.next = &c3;
    c2.data = 31;
    c3.next = NULL;
    c3.data = 97;

    for(p= &c1; p != NULL; p = p -> next)
       printf("%d\n", p -> data);

   return 0;
}



簡単な例2(Simple Example2) - 要素の初期化 -

/*
    構造体の初期化2
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct cell{
    struct cell *next;
    int data;
}cell;

int main(void){

    cell *p;

    // 初期化の順番に注意!
    cell c3 = {NULL, 97};
    cell c2 = { &c3, 31};
    cell c1 = { &c2, 23};

    for(p= &c1; p != NULL; p = p -> next)
       printf("%d\n", p -> data);

   return 0;
}



ヘッダーの導入(Using a Header Node)

/*
    headerの導入
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct cell{
    struct cell *next;
    int data;
}cell;

int main(void){

    cell *p;

    cell c3 = {NULL, 97};
    cell c2 = { &c3, 31};
    cell c1 = { &c2, 23};
    cell header = {&c1, };

    for(p=header.next; p != NULL; p = p -> next)
       printf("%d\n", p -> data);

   return 0;
}

実行結果

23
31
97



要素の追加(Adding an Item)

/*
    要素の追加
*/
#include <stdio.h>
#include < stdlib.h>

typedef struct cell{
    struct cell *next;
    int data;
}cell;

int main(void){

    cell *p;
    cell *pt;

    cell c3 = {NULL, 97};
    cell c2 = { &c3, 31};
    cell c1 = { &c2, 23};
    cell header = { &c1, };

    cell ct;
    ct.data = 123;

    p = &c2;
    pt = &ct;
    pt -> next = p -> next;
    p -> next = pt;

    for(p = header.next; p != NULL; p = p -> next)
       printf("%3d\n", p -> data);

   return 0;
}

実行結果

  23
  31
123
  97

25行目を p = &header; とすると、
123
  23
  31
  97




要素の削除(Removing an Item)

/*
    要素の削除
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct cell{
    struct cell *next;
    int data;
}cell;

int main(void){

    cell *p, *pt;

    cell c3 = {NULL, 97};
    cell c2 = { &c3, 31};
    cell c1 = { &c2, 23};
    cell header = { &c1, };

    pt = &c1;   // c1の次のリスト(すなわちc2)を削除

    p = pt -> next;
    pt -> next = p ->next;
    for(p = header.next; p != NULL; p = p -> next)
       printf("%3d\n", p -> data);

    return 0;
}

実行結果

  23
  97

25行目を p = &header; とすると、
  31
  97
-----------



リストの最後のセルを削除する関数 delete_LC(struct cell *q)

/*
    リストの最後のセルを削除する関数 delete_LC(struct cell *q)
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct cell{
    struct cell *next;
    char name[30];
}cell;

void delete_LC(struct cell *q){
    cell *p;
    p = q -> next;
    if(p -> next == NULL){
       q -> next = NULL;
       printf("%25s\n", p -> name);
       if(strcmp(q -> name,"And Then There Were None!") == 0)
          printf("%20s\n", q -> name);
       return;
    }
    if(p != NULL){
       delete_LC(q -> next);
    }
}

int main(void){

    int i;
    cell c10 ={NULL, "Ten Little Indians"};
    cell c9 = { &c10, "Nine Little Indians"};
    cell c8 = { &c9, "Eight Little Indians"};
    cell c7 = { &c8, "Seven Little Indians"};
    cell c6 = { &c7, "Six Little Indians"};
    cell c5 = { &c6, "Five Little Indians"};
    cell c4 = { &c5, "Four Little Indians"};
    cell c3 = { &c4, "Three Little Indians"};
    cell c2 = { &c3, "Two Little Indians"};
    cell c1 = { &c2, "One Little Indian"};
    cell header = { &c1, "And Then There Were None!"};

    for(i=0; i<10; i++){
       delete_LC(&header);
    }

    return 0;
}

実行結果

Ten Little Indians                               
Nine Little Indians                               
Eight Little Indians                               
Seven Little Indians                               
Six Little Indians                               
Five Little Indians                               
Four Little Indians                               
Three Little Indians                               
Two Little Indians                               
One Little Indian                               
And Then There Were None!