Top Banner
アアアアアアアアアアアア アアアア 11-4 11 アアアアアアアアアアアア アアアア アア アアアアアアア アアアア
31

アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

Feb 08, 2016

Download

Documents

__ZeD__

アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」. 横浜国立大学 理工 学部 数物・電子情報系学科 富井尚志. int main(void) { struct list * listptr , *new1, *new2; listptr = get_list ( ); print_list ( listptr ); new1 = ( struct list *) malloc ( sizeof ( struct list)); new1 -> key = 222; - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

アルゴリズムとデータ構造補足資料 11-4「第 11 回問題の動作」

横浜国立大学理工学部 数物・電子情報系学科富井尚志

Page 2: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;} 画面出力

<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

Page 3: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

Page 4: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

Page 5: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1key

next

Page 6: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1key

next

222

Page 7: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1key

next

222

Page 8: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1key

next

222

x p

Page 9: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1key

next

222

listptr listptr->nextlistptr->next->next

listptr->next->next->next

x p

Page 10: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

key

next

222

x p

Page 11: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

key

next

222

x p

p->next

p

p->next->next

Page 12: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

key

next

222

x p

Page 13: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

key

next

222

x p

Page 14: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

NULLkey

next

1key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

key

next

222

x p

Page 15: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

Page 16: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

Page 17: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

key

next

Page 18: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

key

next

333

Page 19: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

key

next

333

x p

Page 20: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

key

next

333

x p

Page 21: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

listptr

new2

new1

NULLkey

next

1key

next

222

key

next

333

x p

Page 22: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

333

x p

Page 23: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

333

x p

key

next

tmp

Page 24: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

333

x p

key

next

333tmp

Page 25: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

5key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

x p

key

next

333tmp

Page 26: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

333key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

x p

key

next

333tmp

Page 27: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

333key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

x p

key

next

333tmp

Page 28: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

333key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

x p

key

next

333tmp

Page 29: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

333key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

listptr

new2

new1

Page 30: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

333key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

listptr

new2

new1

Page 31: アルゴリズムとデータ構造 補足資料 11-4 「第 11 回問題の動作」

int main(void){ struct list *listptr, *new1, *new2; listptr = get_list( ); print_list( listptr ); new1 = (struct list *)malloc(sizeof(struct list)); new1 -> key = 222; func1( new1, listptr->next->next->next); print_list( listptr ); new2 = (struct list *)malloc(sizeof(struct list)); new2 -> key = 333; func2( new2, listptr->next); print_list( listptr ); return 0;}

void func1(struct list *x, struct list *p){ x->next = p->next->next; p->next->next = x;}

void func2(struct list *x, struct list *p){ struct list tmp; tmp = *x; *x = *p; *p = tmp; p->next = x->next;}

key

next

2key

next

3key

next

4key

next

333key

next

6

画面出力<6><5><4><3><2><1><6><5><4><3><2><222><1><6><333><4><3><2><222><1>

NULLkey

next

1key

next

222

key

next

5

listptr

new2

new1