할당을 위해 입력 텍스트 파일에서 단독 연결 목록을 만들려고합니다. 한 번에 조금씩 해보려하고있어 코드가 완전하지 않다는 것을 알고 있습니다. 나는 헤드 포인터를 만들고 그 값을 인쇄 해 보았고 작동하도록 할 수는 없었지만 그 이유는 잘 모르겠습니다. 구조체, 작성 목록 및 인쇄 목록 함수를 포함했습니다. 나는 그 부분이 작동하기 때문에 열린 파일을 포함시키지 않았다.C에서 단일 연결 목록 만들기
typedef struct List
{
struct List *next; /* pointer to the next list node */
char *str; /* pointer to the string represented */
int count; /* # of occurrences of this string */
} LIST;
LIST *CreateList(FILE *fp)
{
char input[LINE_LEN];
LIST *root; /* contains root of list */
size_t strSize;
LIST *newList; /* used to allocate new list members */
while (fscanf(fp, BUFFMT"s", input) != EOF) {
strSize = strlen(input) + 1;
/* create root node if no current root node */
if (root == NULL) {
if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) {
printf("Out of memory...");
exit(EXIT_FAILURE);
}
if ((char *)malloc(sizeof(strSize)) == NULL) {
printf("Not enough memory for %s", input);
exit(EXIT_FAILURE);
}
memcpy(newList->str, input, strSize); /*copy string */
newList->count = START_COUNT;
newList->next = NULL;
root = newList;
}
}
return root;
}
/* Prints sinly linked list and returns head pointer */
LIST *PrintList(const LIST *head)
{
int count;
for (count = 1; head != NULL; head = head->next, head++) {
printf("%s %d", head->str, head->count);
}
return head; /* does this actually return the start of head ptr, b/c I want to
return the start of the head ptr. */
}
'PrintList'에'head ++ '을 넣고 싶지 않다면,'head = head-> next'는 이미 포인터를 증가시킵니다. –
두 번 묻습니까 ..? http://stackoverflow.com/questions/2309618/single-linked-lists-in-c – lorenzog