双方向リストがわかりません。
具体的には prev の使い方がわからないです。
next は test.h で書いただけで正常に動いているのに対し、prev が動かないのにはどのような理由があるのかを教えていただきたいです。
int list_print_backでStation_doublyリストを後ろからprintしたいのですがプリントされません。
全体の文章が長くなってしまいすみません。
よろしくお願いいたします。
出力
あ(0.0km) い(0.5km) う(1.0km) え(1.5km) お(2.0km)
test.h
1typedef struct station { 2 char name[60]; //最大20文字 3 float dist; 4 struct station_doubly *next; 5 struct station_doubly *prev; 6} Station_doubly; 7 8int list_print(Station_doubly *list); 9int list_print_back(Station_doubly *list); 10Station_doubly *list_init();
list_print_back.c
1#include<stdio.h> 2#include<stdlib.h> 3#include "test.h" 4int list_print_back(Station_doubly *list) { 5 Station_doubly *pt, *pt0; 6 pt0=list; 7 pt = list->next; 8 if (pt==NULL) printf("NULL"); 9 while (pt!=NULL) { 10 pt = pt->next; 11 pt0=pt0->next; 12 } 13 14 while (pt0!=NULL) { 15 printf("%s(%3.1fkm) ", pt0->name, pt0->dist); 16 pt0=pt0->prev; 17 } 18 printf("\n"); 19 return 0; 20};
list_init.c
1#include<stdio.h> 2#include<stdlib.h> 3#include "test.h" 4Station_doubly *list_init() { 5 Station_doubly *list, *pt; 6 list = node_create("dummy", 0.0); //ダミーノード用 7 pt = list; 8 list_insert_doubly(pt, "あ", 0.0); 9 pt = pt->next; 10 list_insert_doubly(pt, "い", 0.5); 11 pt = pt->next; 12 list_insert_doubly(pt, "う", 1.0); 13 pt = pt->next; 14 list_insert_doubly(pt, "え", 1.5); 15 pt = pt->next; 16 list_insert_doubly(pt, "お", 2.0); 17 return list; 18}
list_print.c
1#include<stdio.h> 2#include<stdlib.h> 3#include "test.h" 4int list_print(Station_doubly *list) { 5 Station_doubly *pt; 6 pt = list->next; 7 if (pt==NULL) printf("NULL"); 8 while (pt!=NULL) { 9 printf("%s(%3.1fkm) ", pt->name, pt->dist); 10 pt = pt->next; 11 } 12 printf("\n"); 13 return 0; 14};
main.c
1#include<stdio.h> 2#include<stdlib.h> 3#include "test.h" 4int main(void){ 5 int listmax=20; 6 Station_doubly *list, *pt; 7 list = list_init(); 8 list_print(list); 9 list_print_back(list); 10 return 0; 11}