25
Algorithms and Data Structures on C 連結リスト

連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

連結リスト

Page 2: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

この回の要点

• 連結リストによるリスト

–連結リストの構造

–連結リストの利点と欠点

– C言語による連結リストの実現

• ヘッダファイルによるソースファイルの分割

Page 3: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

連結リスト(linked list)

• リストの実現の一種

• リストに含まれる各要素をリンクによって連結した構造– リンクとは、他のデータへの参照のこと

– 各要素は、自分から次のデータへのリンクを持つ

– その要素が連なった構造が連結リスト

参照リンク

名前

年齢

個人データ

NULL

Aさん

BさんCさん

Dさん Eさん

Fさん

連結リスト

Page 4: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

連結リストの利点と欠点

• 利点– データの挿入・削除が簡単=O(1)

– サイズが可変(メモリの上限はある)

• 欠点– 要素kにアクセスするのにリンクをたどる必要がある=

O(N)

– データ以外にリンク分のメモリが必要

挿入削除

Page 5: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

双方向連結リスト(double linked list)

• 前後の要素へのリンクを持つ連結リスト

– 要素を逆順にたどることが可能

– 2つのリンクを持つためのメモリが必要

参照次へ

名前

年齢

個人データ(PD)

参照前へ

NULL

Aさん

BさんCさん

Dさん Eさん

Fさん

双方向連結リスト

(ListPD)

NULL

Page 6: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

個人情報型PD

参照next

name

age

参照prev

メンバー

char *name 名前

int age 年齢

PD *prev,*next 前後のリンク

関数

PD* makePD(const char*,int) PDの生成

void free(PD*) PDの破棄

void insertBefore(PD*,PD*) 前に挿入

void insertAfter(PD*,PD*) 後に挿入

void remove(PD*) 削除

void print(PD*) 表示

Page 7: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

PD.h

#ifndef __PD__h

#define __PD__h

/***

*** 個人情報***/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 個人情報型の宣言typedef struct PDtag {

struct PDtag *prev,*next; // 前後の個人情報へのリンクchar *name; // 名前int age; // 年齢

} PD;

ヘッダファイルの重複読み込みを回避

続く

Page 8: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

PD.h

// プロトタイプ宣言

PD *makePD(const char *n,int a); // 生成する

void free(PD *pd); // 破棄する

void insertBefore(PD *pd1,PD *pd2);

// pd1の前にpd2を接続する

void insertAfter(PD *pd1,PD *pd2);

// pd1の後にpd2を接続する

void remove(PD *pd); // pdを削除する

void print(PD *pd); // 個人情報を表示する

#endif // __PD__hここまでが、1回しか読み込まれない

Page 9: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

PD.cc

/***

*** PDの実装***/

#include "PD.h"

// 個人情報の生成PD *makePD(const char *n,int a){

PD *pd=(PD*)malloc(sizeof(PD));

pd->prev=pd->next=NULL;

pd->name=(char*)malloc(strlen(n)+1);

strcpy(pd->name,n);

pd->age=a;

return pd;

}

// 個人情報の破棄void free(PD *pd){

free(pd->name);

free((void*)pd);

}続く

名前を入れる分の配列を確保する長さは、文字列の最後のNULLを

含めている。

Page 10: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

PD.cc

// 個人情報pd1の前に個人情報pd2を接続する

void insertBefore(PD *pd1,PD *pd2){

if(pd1==NULL || pd2==NULL) return;

if(pd1->prev)

pd1->prev->next=pd2;

pd2->prev=pd1->prev;

pd2->next=pd1;

pd1->prev=pd2;

}

続く

pd2pd1

pd1前に挿入

Page 11: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

PD.cc

// 個人情報pd1の後ろに個人情報pd2を接続する

void insertAfter(PD *pd1,PD *pd2){

if(pd1==NULL || pd2==NULL) return;

if(pd1->next)

pd1->next->prev=pd2;

pd2->prev=pd1;

pd2->next=pd1->next;

pd1->next=pd2;

}

続く

pd1

pd2

pd1

後に挿入

Page 12: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

PD.cc

// 個人情報pdを削除するvoid remove(PD *pd){

if(pd==NULL) return;

if(pd->prev)

pd->prev->next=pd->next;

if(pd->next)

pd->next->prev=pd->prev;

pd->prev=pd->next=NULL;

}

// 個人情報を表示するvoid print(PD *pd){

printf("%s(%d)",pd->name,pd->age);

}

pd削除

Page 13: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

双方向リンクリストListPD

メンバー

PD *first,*last 先頭と末尾の要素

int size 要素数

関数

ListPD* makeLlistPD() 生成

void free(ListPD*) 破棄

void addTop(ListPD*,PD*) 先頭に追加

void add(ListPD*,PD*) 末尾に追加

void remove(ListPD*,PD*) 要素を削除

PD* get(ListPD*,int) 取り出し

int getSize(ListPD*) 要素数

void print(ListPD*) 表示

PD

PD

PD

PDlast

first

PD

Page 14: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.h

#ifndef __ListPD__h

#define __ListPD__h

/***

*** 個人情報のリスト***/

#include "PD.h"

// 個人情報リスト型の宣言typedef struct {

PD *first,*last; // 先頭と末尾の要素int size; // 要素数

} ListPD;

ヘッダファイルの重複読み込みを回避

続く

Page 15: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.h

// プロトタイプ宣言ListPD *makeListPD(); // 個人情報リストの生成するvoid free(ListPD *lp); // 個人情報リストの破棄するvoid addTop(ListPD *lp,PD *pd);

// リストlpの先頭に個人情報pdを挿入するvoid add(ListPD *lp,PD *pd);

// リストlpの末尾に個人情報pdを追加するvoid remove(ListPD *lp,PD *pd);

// リストlpの要素pdを削除するPD *get(ListPD *lp,int n);

// n番目の要素を得る(先頭は0番)int getSize(ListPD *lp); // 要素数を得るvoid print(ListPD *lp); // リストの内容を表示する

#endif // __ListPD__h

Page 16: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.cc

/***

*** ListPDの実装***/

#include "ListPD.h"

// 個人情報リストの生成ListPD *makeListPD(){

ListPD *lp=(ListPD*)malloc(sizeof(ListPD));

lp->first=lp->last=NULL;

lp->size=0;

return lp;

}// 個人情報リストの破棄void free(ListPD *lp){

free((void*)lp);

}

続く

Page 17: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.cc

// リストlpの先頭に個人情報pdを挿入void addTop(ListPD *lp,PD *pd){

if(lp->first==NULL) // リストが空ならlp->first=lp->last=pd;

else{

insertBefore(lp->first,pd);

lp->first=pd;

}

lp->size++;

}

続く

PD

PD

PD

PDlast

first

PD

pd

last

first

pd

Page 18: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.cc

// リストlpの末尾に個人情報pdを追加void add(ListPD *lp,PD *pd){

if(lp->last==NULL) // リストが空ならlp->first=lp->last=pd;

else{

insertAfter(lp->last,pd);

lp->last=pd;

}

lp->size++;

}

続く

PD

PD

PD

PDlast

first

PD

pdlast

first

pd

Page 19: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.cc

// 要素pdを削除void remove(ListPD *lp,PD *pd){

if(lp->first==pd) lp->first=pd->next;

if(lp->last==pd) lp->last=pd->prev;

remove(pd);

lp->size--;

}

// n番目の要素を得る(先頭は0番)PD *get(ListPD *lp,int n){

PD *p=lp->first;

for(int i=0;i<n && p;i++)

p=p->next;

return p;

}続く

PD

PD

PD

last

first

PD

pd

Page 20: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

ListPD.cc

// 要素数int getSize(ListPD *lp){

return lp->size;

}

// 表示void print(ListPD *lp){

PD *p=lp->first;

printf("ListPD:(%d):¥n",lp->size);

for(int i=1;p;p=p->next,i++){

printf("%d:",i);

print(p);

printf(" ");

}

printf("¥n");

}

Page 21: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

個人情報の双方向リンクリストの利用(TestListPD.cc)

• 3人の個人情報をリストに挿入

• リストの表示

• もう1人を追加

• リストを表示

• 2番目のデータの削除

• リストを表示

Page 22: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

TestListPD.cc

/***

*** 個人情報リストのテスト***/

#include "ListPD.h"

int main(void){

ListPD *lp=makeListPD();

PD *pd;

add(lp,makePD("山田",19));

add(lp,pd=makePD("森",50));

add(lp,makePD("田中",23));

print(lp);

続く

Page 23: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

TestListPD.cc

addTop(lp,makePD("近藤",41));

printf("**before**¥n"); print(lp);

remove(lp,pd);

printf("**after**¥n"); print(lp);

printf("2番目は");

print(get(lp,1));

printf("です。¥n");}

Page 24: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

課題191028

• 小学校の成績管理をするために、児童の成績データを双方向リンクリストに入れて管理する。このとき、SeisekiDataというクラスはどのような構造にすればよいか、考えて示せ。ただし、型のみでよい。(関数は不要)– 成績は国語、算数、理科、社会の4科目

– 児童の情報は、学年、クラス、名前

• 作成方法:自分で考えたクラス構造を、プログラムコードとしてワード文書内に書くこと。ワードのファイル名は、

“scXXXXXX-al191028.docx”

• 提出方法:メールで。•メールの本文中にも学籍番号を氏名を明記すること。

• 提出先:[email protected]

• メールタイトル:”アルゴリズム課題191028” ← 厳守!• 期限:2019年11月4日(月・祝) 24:00

Page 25: 連結リストfuchida/lecture/algorithm/alg...Algorithms and Data Structures on C 連結リスト(linked list) • リストの実現の一種 • リストに含まれる各要素をリンクによって連結した構造

Algorithms and Data Structures on C

連結リスト終了