안녕하세요!
C언어에서 연결리스트 부분 공부중입니다.
사람의 나이, 키, 몸무게를 원하는 만큼 넣는것은 함수화 했는데, 출력하는걸 함수화하지 못해서 고민입니다.
#include <stdio.h>
#include <malloc.h>
typedef struct info
{
unsigned short int age;
float height;
float weight;
struct info *p_next;
} INFO;
void AddInfo(INFO **qq_head, INFO **qq_tail, unsigned short int age,
float height, float weight);
/* void PrintfInfo(INFO *qq_head);*/
void main()
{
INFO *p_head = NULL, *p_tail = NULL, *p;
unsigned short age;
float height, weight;
char count = 0;
char go = 0;
while (1)
{
printf("input datas (1:go | else:stop) :");
scanf("%d", &go);
if (1 == go)
{
printf("%dth age :", count + 1);
scanf("%hu", &age);
printf("%dth height : ", count + 1);
scanf("%f", &height);
printf("%dth weight : ", count + 1);
scanf("%f", &weight);
AddInfo(&p_head, &p_tail, age, height, weight);
count++;
}
else
{
break;
}
}
count = 0;
p = p_head;
while (NULL != p)
{
printf("====%dth person====\n", count + 1);
printf("age : %d\n", p->age);
printf("height : %5.2f\n", p->height);
printf("weight : %5.2f\n", p->weight);
p = p->p_next;
count++;
}
printf("====================\n");
}
void AddInfo(INFO **qq_head, INFO **qq_tail,
unsigned short int age, float height, float weight)
{
if (NULL != *qq_head)
{
(*qq_tail)->p_next = (INFO *)malloc(sizeof(INFO));
*qq_tail = (*qq_tail)->p_next;
}
else
{
*qq_head = (INFO *)malloc(sizeof(INFO));
*qq_tail = *qq_head;
}
(*qq_tail)->age = age;
(*qq_tail)->height = height;
(*qq_tail)->weight = weight;
(*qq_tail)->p_next = NULL;
}
/* void PrintfInfo(INFO *qq_head)
{
char count = 0;
while (NULL != qq_head)
{
printf("====%dth person====\n", count + 1);
printf("age : %d\n", qq_head->age);
printf("height : %5.2f\n", qq_head->height);
printf("weight : %5.2f\n", qq_head->weight);
qq_head = qq_head->p_next;
count++;
}
printf("====================\n");
}
*/
이 코드에서 PrintfInfo 함수를 만들어서 하려고 했는데...생각처럼 잘 되지 않았습니다.
함수를 어떻게 구성해야 좋을까요?
감사합니다.