/*広告*/

/*[C言語]サンプルプログラム集 単方向リスト*/

/*目次へ戻る*/

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> struct LIST{ char name[128]; char num[128]; struct LIST* next; }; struct LIST *head; struct LIST *tail; void add_head(struct LIST ); void add_tail(struct LIST ); void del_head(void); void del_tail(void); int get_count(void); void replace_at(struct LIST ,int ); void del_at(int ); void insert_at(struct LIST ,int ); void disp_list(void); void free_list(void); int main(){ struct LIST input_data; int num; int flag = 1; while(flag){ printf("どうしますか?\n"); printf("1)先頭に登録 2)末尾に登録 3)先頭を削除 4)末尾を削除\n"); printf("5)リストの総数 6)指定番号置換 7)指定番号削除 8)指定番号挿入\n"); printf("9)表示 10)画面消去 11)終了 > "); (void)scanf("%d",&num); switch(num - 1){ case 0: printf("名前を登録してください > "); (void)scanf("%s",input_data.name); printf("電話番号を登録してください > "); (void)scanf("%s",input_data.num); add_head(input_data); printf("\n"); disp_list(); printf("\n"); break; case 1: printf("名前を登録してください > "); (void)scanf("%s",input_data.name); printf("電話番号を登録してください > "); (void)scanf("%s",input_data.num); add_tail(input_data); printf("\n"); disp_list(); printf("\n"); break; case 2: del_head(); disp_list(); printf("\n"); break; case 3: del_tail(); disp_list(); printf("\n"); break; case 4: disp_list(); printf("リストの総数 = %d\n\n",get_count()); break; case 5: disp_list(); printf("何番目のデータを置き換えますか > "); (void)scanf("%d",&num); if(num >= 1 && num <= get_count()){ printf("%d番目のデータを置き換えます!\n",num); printf("名前を登録してください > "); (void)scanf("%s",input_data.name); printf("電話番号を登録してください > "); (void)scanf("%s",input_data.num); replace_at(input_data,num); printf("\n"); disp_list(); printf("\n"); } else{ printf("無効な値です!\n\n"); } break; case 6: disp_list(); printf("何番目のデータを削除しますか > "); (void)scanf("%d",&num); if(num >= 1 && num <= get_count()){ printf("%d番目のデータを削除します!\n",num); del_at(num); printf("\n"); disp_list(); printf("\n"); } else{ printf("無効な値です!\n\n"); } break; case 7: disp_list(); printf("何番目にデータを挿入しますか > "); (void)scanf("%d",&num); if(num >= 1 && num <= get_count()){ printf("%d番目にデータを挿入します!\n",num); printf("名前を登録してください > "); (void)scanf("%s",input_data.name); printf("電話番号を登録してください > "); (void)scanf("%s",input_data.num); insert_at(input_data,num); printf("\n"); disp_list(); printf("\n"); } else{ printf("無効な値です!\n\n"); } break; case 8: disp_list(); printf("\n"); break; case 9: system("cls"); break; case 10: flag = 0; break; default: break; } } printf("終了しました!"); free_list(); return 0; } void add_head(struct LIST input_data){ struct LIST* p; if(head != NULL){ p = (struct LIST*)malloc(sizeof(struct LIST)); *p = input_data; p->next = head; head = p; } else{ add_tail(input_data); } } void add_tail(struct LIST input_data){ struct LIST* p; p = (struct LIST*)malloc(sizeof(struct LIST)); if(tail == NULL){ head = p; } else{ tail->next = p; } tail = p; *tail = input_data; tail->next = NULL; } void del_head(){ struct LIST* p; if(head != NULL){ p = head->next; free(head); head = p; if(head == NULL){ tail = NULL; } } } void del_tail(){ struct LIST* p; if(tail != NULL){ p = head; while(p != NULL){ if(p->next == tail){ p->next = NULL; break; } p = p->next; } free(tail); tail = p; if(tail == NULL){ head = NULL; } } } int get_count(){ struct LIST* p; int n = 0; if(head != NULL){ p = head; while (p != NULL){ n++; p = p->next; } return n; } else{ return n; } } void replace_at(struct LIST input_data,int n){ struct LIST* p; struct LIST* p2; int count = 0; if(head != NULL && n >= 1 && n <= get_count()){ p = head; while(p != NULL){ if(count == n - 1){ p2 = p->next; *p = input_data; p->next = p2; break; } else{ count++; p = p->next; } } } } void del_at(int n){ struct LIST* p; struct LIST* p2; int count = 0; if(head != NULL && n >= 1 && n <= get_count()){ if(n == 1){ del_head(); } else{ p = head; p2 = head->next; while(p2 != NULL){ if(count == n - 2){ p->next = p2->next; free(p2); break; } else{ count++; p = p2; p2 = p2->next; } } } } } void insert_at(struct LIST input_data,int n){ struct LIST* p; struct LIST* p2; int count = 0; if(head != NULL && n >= 1 && n <= get_count()){ if(n == 1){ add_head(input_data); } else{ p = head; while(p != NULL){ if(count == n - 2){ p2 = (struct LIST*)malloc(sizeof(struct LIST)); *p2 = input_data; p2->next = p->next; p->next = p2; break; } else{ count++; p = p->next; } } } } } void disp_list(){ struct LIST* p; int n = 0; p = head; printf("\n名前 電話番号\n"); while(p != NULL){ printf("%d:%s %s\n",n + 1,p->name,p->num); n++; p = p->next; } } void free_list(){ struct LIST* p; struct LIST* p2; p = head; while(p != NULL){ p2 = p->next; free(p); p = p2; } head = NULL; tail = NULL; }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

/*Copyright 2016 K.N/petitetech.com*/

/*広告*/