|
linked list에 대한 질문입니다.
포인터 선언은 제대로 한 것 같은데 메모리 할당이 제대로 않됬는지
아니면 출력시 잘 못 불렀는지 계속 이상한 문자만 출력이 되네요.
고수님의 의견을 듣고 싶습니다. solution과 함께...
#include <stdio.h>
typedef struct record {
char num;
struct record *next;
} lpnum;
void main()
{
lpnum *link, *head, *temp;
double *addr;
int cnt = 1;
char ch;
clrscr();
head = (lpnum *)malloc(sizeof(lpnum));
printf("\nhead address %d \n", head);
while (ch != '\r')
{
printf("\nPlease insert any number(0-9): %s");
ch = getche();
if (cnt == 1)
{
head->num = ch;
temp = (lpnum *)malloc(sizeof(lpnum));
link = temp;
head->next = temp;
printf("\nThe next points to %d ", head->next);
cnt = 10;
}
else
{
link->num = ch;
temp = (lpnum *)malloc(sizeof(lpnum));
link->next = temp;
link = temp;
printf("\nCurrent input is %c\n", link->num);
printf("\nThe next points to %d\n", link->next);
}
if (ch == '\r') head->next = '\0';
}
head = addr;
printf("\n Numbers are : ");
while (head->num != '\r')
{
printf(" %c(%d) \n", head->num, head);
head = head->next;
printf(" Numbers are : ");
}
printf("\n\nGood Bye\n\n");
free(head);
free(link);
free(temp);
}
|