• 検索結果がありません。

アルゴリズム論 (第9回)

N/A
N/A
Protected

Academic year: 2021

シェア "アルゴリズム論 (第9回)"

Copied!
23
0
0

読み込み中.... (全文を見る)

全文

(1)

アルゴリズム論

(第9回)

佐々木研(情報システム構築学講座)

講師

山田敬三

[email protected]

(2)

線形連結リスト

(3)

配列

リストの途中への追加,削除は困難

データ4 データ3.5 データ5 データ4

0 データ1 データ2 データ3 N

データ5

データ3.5

(4)

線形連結リスト (Linked List)

各要素をポインタで結合

相対的に参照

配列の要素番号のようなインデックスはない

「何番目のデータ」という絶対的な参照は不可

(5)

線形連結リスト (Linked List)

struct cell { int value;

struct cell *next;

};

value pointer

セル

データ,及び,次の要素の格納場所を示

すポインタで構成

最後のデータの,次を示すポインタは NULL

セル

(6)

線形連結リスト (Linked List)

0xefff920 23 cell

0xefff928 0xefff928 35

0xefff930

0xefff930 48 NULL

23 35 48

0xefff920 0xefff928 0xefff930

(NULL)

struct cell { int value;

struct cell *next;

};

(7)

セルの挿入

(8)

セルを挿入する関数

挿入する位置を示すポインタ型変数へのポインタと,

格納する値を引数とする.

※ポインタ自身を変更する必要があるため

void insert_cell(struct cell

**pointer, int new_value)

(9)

セルを挿入する関数

1.新たなセルを用意し,値(value)を代入

new_cell=(struct cell *)malloc (sizeof(struct cell));

new_cell->value=new_value;

(10)

セルを挿入する関数

2. 新たなセルの次へのポインタに,元の場所へのポイ ンタをコピー

new_cell->next=*pointer;

(NULL) copy

(NULL) new_cell

new_cell

(11)

セルを挿入する関数

3. 元のポインタの値を新たなセルに変更

*pointer=new_cell;

(NULL)

(NULL) new_cell

new_cell change

(12)

セルの挿入

struct cell *head=NULL, **p;

p=&head;

while(…) {

insert_cell(p,data);

p=&((*p)->next);

}

引数

挿入場所のアドレスを格納している変数 のアドレス

つまり,前のセルの次へのポインタが格 納されている変数(next)のアドレス

(13)

セルの削除

(14)

セルを削除する関数

削除するセルへのポインタ型変数へのポインタを引数と する.

つまり,前のセルの,次のセルへのポインタのアドレス

※ポインタ自身を変更する必要があるため

void delete_cell(struct cell **pointer)

(15)

セルを削除する関数

1. 削除したいセルへのポインタを求め,元のポインタの 値を,削除するセルの後のセルに変更

target=*pointer;

*pointer=target->next;

(NULL) target

copy

(NULL) target

(16)

セルを削除する関数

2. セルを削除

free(target);

(NULL) target

(NULL) delete

(17)

循環・重連結リスト

(18)

循環リストと重連結リスト

循環リスト(circular list

重連結リスト(doubly linked list)

(19)

循環・重連結リスト

dummy

空の循環・重連結リスト

(20)

循環・重連結リスト

ノードの挿入

ノードの削除

*(p->left) p

q

q

p

p

(21)

循環・重連結リスト

・関数 insert_cdl_list を実行

・初期状態

typedef struct Node { int num;

struct Node *left, *right;

} node;

int insert_cdl_list(int x) {

node *q, *p = (node*)malloc(sizeof(node));

if (p == NULL) return 0;

p->num = x;

q = start->left;

start->left = p;

p->right = start;

q->right = p;

p->left = q;

return 1;

}

int insert_left(node *p, int x) {

node *q = (node*)malloc(sizeof(node));

if (q == NULL) return 0;

q->left = p->left;

q->num = x;

q->right = p;

p->left->right = q;

p->left = q;

return 1;

}

start

start

p

q

(22)

問題1

・再度,関数 insert_cdl_list 実行した結果を答えなさい.

・解答

typedef struct Node { int num;

struct Node *left, *right;

} node;

int insert_cdl_list(int x) {

node *q, *p = (node*)malloc(sizeof(node));

if (p == NULL) return 0;

p->num = x;

q = start->left;

start->left = p;

p->right = start;

q->right = p;

p->left = q;

return 1;

}

int insert_left(node *p, int x) {

node *q = (node*)malloc(sizeof(node));

if (q == NULL) return 0;

q->left = p->left;

q->num = x;

q->right = p;

p->left->right = q;

p->left = q;

return 1;

}

start

q p

start

q p

(23)

問題2

・関数 insert_left を実行した 結果を答えなさい.

・解答

typedef struct Node { int num;

struct Node *left, *right;

} node;

int insert_cdl_list(int x) {

node *q, *p = (node*)malloc(sizeof(node));

if (p == NULL) return 0;

p->num = x;

q = start->left;

start->left = p;

p->right = start;

q->right = p;

p->left = q;

return 1;

}

int insert_left(node *p, int x) {

node *q = (node*)malloc(sizeof(node));

if (q == NULL) return 0;

q->left = p->left;

q->num = x;

q->right = p;

p->left->right = q;

p->left = q;

return 1;

}

q

p

q

p

参照

関連したドキュメント

These allow us to con- struct, in this paper, a Randers, Kropina and Matsumoto space of second order and also to give the L-dual of these special Finsler spaces of order two,

In Section 3, we employ the method of upper and lower solutions and obtain the uniqueness of solutions of a two-point Dirichlet fractional boundary value problem for a

(1999) “A novel, quantitative model for study of endothelial cell migration and sprout formation within three-dimensional collagen matrices”, Microvasc. 57, 118 – 133) carried out

These adhesive functions are likely to be different outside and inside the basal lamina cylinder surrounding muscle fibers, because the molecular components of these

Before discussing p-adic L-functions we will develop Fourier theory for the multiplicative group; this will be useful because the p-adic L-functions we con- struct arise as

First, a similar technique allows one to con- struct linear algebras for different types of extended extrafunctions (pointwise, compact- wise, and extended distributions) with

LC06111TMT Battery Protection Controller with Integrated MOSFET, 1-Cell Lithium-Ion LC05711ARA Battery Protection Controller with Integrated MOSFET, 1-Cell Lithium-Ion

Charge Curve, I INLIM Limits I OCHARGE Assuming that V OREG is programmed to the cell’s fully charged “float” voltage, the current that the battery accepts with the PWM