Last active
July 2, 2017 15:10
-
-
Save KeitetsuWorks/c0d817134f65139cbabbda83d96cb572 to your computer and use it in GitHub Desktop.
連結リストを使用したサンプルプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file PersonalData.c | |
* @brief 連結リストを使用したサンプルプログラム | |
* @author Keitetsu | |
* @date 2009/02/23 | |
* @copyright Copyright (c) 2009 Keitetsu | |
*/ | |
#include <stdio.h> | |
#include <stdio_ext.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct PersonalData_st { | |
char name[21]; | |
char address[21]; | |
struct PersonalData_st *next; | |
} PERSONALDATA_T; | |
const char *PersonalData_cmd_input_end = "end"; | |
static void PersonalData_clearData( | |
PERSONALDATA_T *data); | |
static int PersonalData_inputData( | |
PERSONALDATA_T *data); | |
static int PersonalData_addDataToList( | |
PERSONALDATA_T **list_root, | |
const PERSONALDATA_T data); | |
static void PersonalData_printList( | |
PERSONALDATA_T **list_root); | |
static void PersonalData_deleteList( | |
PERSONALDATA_T **list_root); | |
int main(void) | |
{ | |
PERSONALDATA_T *list_root; | |
PERSONALDATA_T input_buffer; | |
int retval; | |
/* 個人データリストを作成 */ | |
printf("個人データリストを作成します。\n"); | |
list_root = NULL; | |
/* 個人データの入力と個人データリストへの追加 */ | |
printf("個人データの入力を開始します。\n"); | |
printf("入力を終了するときは NAME に %s と入力してください。\n", | |
PersonalData_cmd_input_end); | |
while (1) { | |
/* 個人データの入力 */ | |
PersonalData_clearData(&input_buffer); | |
retval = PersonalData_inputData(&input_buffer); | |
if (retval != 0) { | |
printf("個人データの入力を終了しました。\n"); | |
break; | |
} | |
/* 個人データリストへの追加 */ | |
retval = PersonalData_addDataToList(&list_root, input_buffer); | |
if (retval != 0) { | |
printf("個人データリストへの追加を中止します。\n"); | |
break; | |
} | |
} | |
/* 個人データリストを表示 */ | |
printf("個人データリストを表示します。\n"); | |
PersonalData_printList(&list_root); | |
/* 個人データリストを削除 */ | |
printf("個人データリストを削除します。\n"); | |
PersonalData_deleteList(&list_root); | |
return 0; | |
} | |
static void PersonalData_clearData( | |
PERSONALDATA_T *data) | |
{ | |
memset(data->name, '\0', sizeof(data->name)); | |
memset(data->address, '\0', sizeof(data->address)); | |
return; | |
} | |
static int PersonalData_inputData( | |
PERSONALDATA_T *data) | |
{ | |
int retval; | |
int flag_input_end; | |
printf("NAME: "); | |
__fpurge(stdin); | |
scanf("%20[^\n]", data->name); | |
retval = strncmp( | |
data->name, | |
PersonalData_cmd_input_end, | |
sizeof(PersonalData_cmd_input_end)); | |
if (retval == 0) { | |
flag_input_end = 1; | |
} | |
else { | |
printf("ADDRESS: "); | |
__fpurge(stdin); | |
scanf("%20[^\n]", data->address); | |
flag_input_end = 0; | |
} | |
printf("----\n"); | |
return flag_input_end; | |
} | |
static int PersonalData_addDataToList( | |
PERSONALDATA_T **list_root, | |
const PERSONALDATA_T data) | |
{ | |
PERSONALDATA_T *tmp; | |
int result; | |
tmp = (PERSONALDATA_T *)malloc(sizeof(PERSONALDATA_T)); | |
if (tmp == NULL) { | |
printf("Error: 新規データ用のメモリ確保に失敗しました。\n"); | |
result = 1; | |
} | |
else { | |
*tmp = data; | |
tmp->next = *list_root; | |
*list_root = tmp; | |
result = 0; | |
} | |
return result; | |
} | |
static void PersonalData_printList( | |
PERSONALDATA_T **list_root) | |
{ | |
PERSONALDATA_T *tmp; | |
tmp = *list_root; | |
while (tmp != NULL) { | |
printf("NAME: %s\n", tmp->name); | |
printf("ADDRESS: %s\n", tmp->address); | |
printf("----\n"); | |
tmp = tmp->next; | |
} | |
return; | |
} | |
static void PersonalData_deleteList( | |
PERSONALDATA_T **list_root) | |
{ | |
PERSONALDATA_T *tmp; | |
while (*list_root != NULL) { | |
tmp = *list_root; | |
*list_root = tmp->next; | |
free(tmp); | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment