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

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

N/A
N/A
Protected

Academic year: 2021

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

Copied!
25
0
0

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

全文

(1)

アルゴリズム論

(第9回)

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

講師 山田敬三

[email protected]

(2)

線形連結リスト

(3)

配列

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

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

0 データデータデータ 123 N

データ 5

データ 3.5

(4)

線形連結リスト (Linked List)

各要素をポインタで結合

先頭から順に参照

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

「何番目のデータ」というランダムアクセスは不可

5 3 8 2

(5)

線形連結リスト (Linked List)

struct cell { int value;

struct cell *next;

};

セル

データ,及び,次の要素の格納場所を 示すポインタで構成

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

セル

v a lu e p o i n t e r

(6)

線形連結リスト (Linked List)

struct cell { int value;

struct cell *next;

};

c e l l 0 x e f f f 9 2 0 2 3

0 x e f f f 9 2 8 0 x e f f f 9 2 8 3 5

0 x e f f f 9 3 0

0 x e f f f 9 3 0 4 8 N U L L

2 3 3 5 4 8

0 x e f f f 9 2 0 0 x e f f f 9 2 8 0 x e f f f 9 3 0

( N U L L )

(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;

( N U L L ) c o p y

( N U L L ) n e w _ c e l l

n e w _ c e l l

(11)

セルを挿入する関数

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

*pointer=new_cell;

( N U L L )

( N U L L ) n e w _ c e l l

n e w _ c e l l c h a n g e

(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;

( N U L L ) t a r g e t

c o p y

( N U L L ) t a r g e t

(16)

セルを削除する関数

2. セルを削除

free(target);

( N U L L ) t a r g e t

( N U L L ) d e l e t e

(17)

循環・重連結リスト

(18)

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

循環リスト( circular list

重連結リスト( doubly linked list

(19)

循環・重連結リスト

dummy

空の循環・重連結リスト

(20)

循環・重連結リスト

ノードの挿入

ノードの削除

(p->left) p

q

q p

p

(21)

循環・重連結リスト

・関数 insert_cdl_list を実行

・初期状態

(22)

問題1

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

・解答

(23)

問題2

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

・解答

参照

関連したドキュメント

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