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

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

N/A
N/A
Protected

Academic year: 2021

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

Copied!
27
0
0

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

全文

(1)

アルゴリズム論

(第3回)

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

講師 山田敬三

[email protected]

(2)

リストと木構造

(3)

リスト

順序付けされたデータの並び

一次元配列もリストの一種

データの追加,削除,参照,置き換えなどが行われる.

それぞれ関数化しておくと便利

操作方法に応じたデータ構造が必要

(4)

スタック

データを順番に追加

最後に追加された データから順番に取 り出す.

例)食堂のトレー

• LIFO (Last In First Out)

0 データ1

データ2 データ3 データ4 データ5

SP

SP

SP

SP

SP

SP

SP

SP N

SP

SP

(5)

0 N

キュー(待ち行列)

最初に追加したデータ を最初に取り出す.

例)映画館のチケット 売り場

• FIFO (First In First Out)

データデータデータ345

データ1 データ2

Head Tail HeadHeadHeadHead Tail

Tail Tail Tail Tail

(6)

スタックの実装

宣言

データを蓄えておくためのリスト

データの追加,取り出し位置を示すポインタ

/* データを蓄積するリスト */

float stack[STACKSIZE];

/* スタックポインタ */

int sp = 0;

(7)

push (データの追加)

int push(float data) { if(sp>=STACKSIZE)

return 0;

else {

stack[sp++]=data;

return 1;

}

} /* 返却値は、成功時1、失敗時0 */

ST

(8)

pop ( データの取り出し )

int pop(float *data) { if(sp<=0)

return 0;

else {

*data=stack[--sp];

return 1;

}

} /* 返却値は、成功時1、失敗時0 */

ST

(9)

逆ポーランド記法

演算子を対象項の後ろに記述

後置記法

スタック構造により実現可能

かっこ操作が必要ない.

1.2×(3.4+1.6) 1.2 3.4 1.6 + ×

(10)

逆ポーランド記法の演算

数値なら

• push

演算子なら

• 2

pop

演算結果を

push

最終的に残ったも のが答え

0 1.23.41.6

SP

SP

SP

SP N

5.06.0

SP

SP

SP 1.2×(3.4+1.6)

1.2 3.4 1.6 + ×

SP

SP

SP

(11)

線形連結リスト

(12)

配列

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

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

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

データ5

データ3.5

(13)

線形連結リスト (Linked List)

各要素をポインタで結合

相対的に参照

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

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

(14)

セルの挿入

(15)

セルの削除

(16)

線形連結リスト (Linked List)

セル

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

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

NULL

struct cell { int value;

struct cell *next;

};

value pointer

セル

(17)

線形連結リスト (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;

};

(18)

セルの挿入

(19)

セルを挿入する関数

挿入する位置を示すポインタへのポインタと,格納する 値を引数とする.

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

void insert_cell(struct cell

**pointer, int new_value)

(20)

セルを挿入する関数

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

new_cell->value=new_value;

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

(21)

セルを挿入する関数

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

new_cell->next=*pointer;

(NULL) copy

(NULL) new_cell

new_cell

(22)

セルを挿入する関数

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

*pointer=new_cell;

(NULL)

(NULL) new_cell

new_cell change

(23)

セルの挿入

引数

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

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

struct cell *head=NULL, **p;

p=&head;

while(…) {

insert_cell(p,data);

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

}

(24)

セルの削除

(25)

セルを削除する関数

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

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

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

void delete_cell(struct cell **pointer)

(26)

セルを削除する関数

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

target=*pointer;

*pointer=target->next;

(NULL) target

copy

(NULL) target

(27)

セルを削除する関数

2. セルを削除

free(target);

(NULL) target

(NULL) delete

参照

関連したドキュメント

Histologic appearance varies markedly from area to area in the same case, varying from vascular granulation tissue heavily in filtrated with both plasma cells and lymphocytes to

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