0% found this document useful (0 votes)
19 views37 pages

ノート

this is about Data structure

Uploaded by

masaki0827.st
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views37 pages

ノート

this is about Data structure

Uploaded by

masaki0827.st
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

* の説明

* メモリ割り付けの説明

int num_cars = 3; // size of the car array

struct Car** car_arr = malloc(sizeof(struct Car*) * num_cars );


ポインタ変数の使用方法
配列とポインタ 配列とポインタの違い
*struct Car* 型の説明 1

int num_cars = 3; // size of the car array

struct Car** car_arr = malloc(sizeof(struct Car*) * num_cars );


*struct Car* 型の説明 2

1.car_arr に対してメモリを確保:
struct Car** car_arr = (struct Car**) malloc(sizeof(struct Car*) *
3);
これにより、 Car 構造体へのポインタを 3 つ格納できるメモリ領域が確保され、キャストによって car_arr は **struct Car** 型とし
て扱えるようになります。
2. メモリの状態 ( キャスト後 )
car_arr:
+--------------------+ +--------------------+ +--------------------+
| car_arr[0] ( 未初期化 ) | | car_arr[1] ( 未初期化 ) | | car_arr[2] ( 未初期化 ) |
+--------------------+ +--------------------+ +--------------------+

3. 個別に Car 構造体のメモリを割り当て

car_arr[0] = malloc(sizeof(struct Car));


car_arr[1] = malloc(sizeof(struct Car));
car_arr[2] = malloc(sizeof(struct Car));
*struct Car* 型の説明 3
car_arr:
+--------------------+ +--------------------+ +--------------------+
| car_arr[0] -----> 0x100 | | car_arr[1] -----> 0x200 | | car_arr[2] -----> 0x300 |
+--------------------+ +--------------------+ +--------------------+

0x100 (Car 構造体 )


+--------------------+
| make: ( 未初期化 ) |
| year: ( 未初期化 ) |
| price: ( 未初期化 ) |
+--------------------+
ポインタポインタを使用して Car 構造体にアクセス可能
0x200 (Car 構造体 )
+--------------------+
| make: ( 未初期化 ) |
| year: ( 未初期化 ) |
| price: ( 未初期化 ) |
+--------------------+

0x300 (Car 構造体 )


+--------------------+
| make: ( 未初期化 ) |
| year: ( 未初期化 ) |
| price: ( 未初期化 ) |
+--------------------+
*struct Car* 型の説明 3
car_arr:
+--------------------+ +--------------------+ +--------------------+
| car_arr[0] -----> 0x100 | | car_arr[1] -----> 0x200 | | car_arr[2] -----> 0x300 |
+--------------------+ +--------------------+ +--------------------+

0x100 (Car 構造体 )


+--------------------+
| make: ( 未初期化 ) |
| year: ( 未初期化 ) |
| price: ( 未初期化 ) |
+--------------------+
ポインタポインタを使用して Car 構造体にアクセス可能
0x200 (Car 構造体 )
+--------------------+
| make: ( 未初期化 ) |
| year: ( 未初期化 ) |
| price: ( 未初期化 ) |
+--------------------+

0x300 (Car 構造体 )


+--------------------+
| make: ( 未初期化 ) |
| year: ( 未初期化 ) |
| price: ( 未初期化 ) |
+--------------------+
*struct Car* 型の説明 3
This function should free the memory associated with a dynamic array. In
* particular, while this function should free up all memory used in the array
* itself (i.e. the underlying `data` array), it should not free any memory
* allocated to the pointer values stored in the array. In other words, this
* function does not need to *traverse* the array and free the individual
* elements. This is the responsibility of the caller.
bubbleSort の流れ
** と * の使い分け 1

**void はどんな型でも受け取れる。
*void はその都度気をつける
** と * の使い分け 2
** と * の使い分け 3

異なるデータを管理したかったら void** 。なぜなら void**


は複数あるポインターのポインターを表しており配列になっているから
void と void*1
void と void*2
ダイナミックアレイと **0
ダイナミックアレイと **1
ポインタ生成

データ格納

ポインターを渡してアクセル
ダイナミックアレイと **2
da と insert 関数とタイミング
da と insert 関数とタイミング
Pass by reference
My gusee

Struct node Struct list Struct stack Struct stack


0x3000 0x2000 0x1000
Void* val
Strut node*
Strut node* Struct list* list list
head
next
My gusee

Struct node Struct list Struct stack Struct stack


0x3000 0x2000 0x1000
Void* val
Strut node*
Strut node* Struct list* list list
head
next
スタックとリンクリストの関係を示すコード例
スタックとリンクリストの関係を示すコード例
void* means
List->head いつ生成されるか

= address get!

Struct list Struct node temp(first) If second node make,


(x10) (x100) NULL
Void* val
Strut node*
Strut node*
head
next
Initial NULL Temp= address
get(x100)
Temp->next= list head
List->head= tmp
Logical indeces
NULL means
Sort algo,binary,linear
Recursive function e.x node= 3
node->left = 1, node->right = 4

node->left = 1 の時 left_size=0(NULL なので ),
left_size=0(NULL なので ) で return 1+0+0=1 が返ってくる
node-right = 4 の時 left_size=0, left_size=0 で return
1+0+0=1 が返ってくる
結果 node3 の時 return 1 + 1 + 1 = 3
e.x node= 8
node->left = NULL, node->right = 9

Node->left =0(NULL なので ), node->right = 9 の時
left_size=0, left_size=0 で return 1+0+0=1 が返ってくる
Node8 の時 return 1 + 0 + 1=2

e.x node= 5
node->left = 3, node->right = 8

Node->left =3 の時 3 が帰ってきて
node->right = 8 の時 2 が帰ってくて
Return 1 + 3 + 2=6
Recursive function e.x node= 1
node->left == NULL, node->right == NULL

NULL の時は 0 が返ってくるので
node-right = 1 の時 left_size=0, left_size=0 で return
1+0+0=1 が返ってくる
結果 node3 の時 return 1 + 0 + 0 = 1
e.x node= 4
node->left == NULL, node->right == NULL

NULL の時は 0 が返ってくるので
node-right = 1 の時 left_size=0, left_size=0 で return
1+0+0=1 が返ってくる
結果 node3 の時 return 1 + 0 + 0 = 1
e.x node= 9
node->left == NULL, node->right == NULL

NULL の時は 0 が返ってくるので
node-right = 1 の時 left_size=0, left_size=0 で return
1+0+0=1 が返ってくる
結果 node3 の時 return 1 + 0 + 0 = 1
Free のタイミングと考え方
Bst と bst_node を分ける訳

I,g
** のイメージ 11/19
Dereferencing1
Dereferencing2

You might also like