【发布时间】:2010-01-21 03:21:54
【问题描述】:
我有一个教授要我们做的简单任务。 基本上是从文本文件中提取一些数字并加载到链接列表中。 我不想谈太多细节,但我有一个基本问题。
他为我们提供了这样的功能:
INTLIST* init_intlist( int n )
{
INTLIST *lst;
lst = (INTLIST *)malloc(sizeof(INTLIST));
lst->datum = n;
lst->next = NULL;
return lst;
}
该函数用于初始化链表的第一个元素。然后他要求我们用这个签名定义一个函数:
int insert_intlist( INTLIST *lst, int n )
所以我假设他只是想让我们添加到链接列表中,所以我尝试了这个:
int insert_intlist( INTLIST *lst, int n )
{
INTLIST* lstTemp;
lstTemp = (INTLIST *)malloc(sizeof(INTLIST));
lstTemp->datum = n;
lstTemp->next = lst;
lst = lstTemp;
free(lstTemp);
}
所以我的想法是它创建一个临时节点,分配数据值(Datum)并分配下一个指针指向当前指针指向的位置。然后我将主指针重新分配给这个新创建的临时节点。
这样我们就有了例如 2 个节点:
[新建临时节点] -> [上一个初始化节点]
当我单步执行代码时,它看起来很棒......
然后回到 main 我只有一个打印列表的函数:
while (lst!=NULL)
{
printf("The value is:%d", lst->datum);
lst=lst->next;
}
问题是这似乎只打印一个数字(即我从文件中读取的第一个数字,我认为它是列表中的最后一个,或者至少我认为它是列表中的最后一个)。
但它应该会继续进行,因为我的文件中有 10 位数字。我知道代码很脏,我会清理它...如果有人需要更多信息,这是我的整个主要功能:
#include <stdio.h>
#include <stdlib.h>
#include "intlist.h"
int main(int argc, char *argv[])
{
char c; /* Character read from the file. */
FILE* ptr; /* Pointer to the file. FILE is a
structure defined in <stdio.h> */
int index=0;
//INTLIST* aList[10]; //will use later
/* Open the file - no error checking done */
ptr = fopen("1.txt","r");
/* Read one character at a time, checking
for the End of File. EOF is defined
in <stdio.h> as -1 */
if(ptr==NULL) {
printf("Error: can't open file.\n");
/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
return 1;
}
//aList[index] = malloc(sizeof(INTLIST)); WE NEED THIS LATER ON....
INTLIST *lst=NULL;
while ((c = fgetc(ptr)) != EOF)
{
if (c != ' ')
{
//make sure it isnt a space
int i = c - '0'; //get the value from the text file
if(c=='\n')
{
// aList[index]=lst;
// index++;
// aList[index] = malloc(sizeof(INTLIST));
while (lst!=NULL)
{
printf("The value is:%d", lst->datum);
lst=lst->next;
}
free(lst);
free(aList[index]);
return 0;
//new line in the file
//create another linked list
}
if (lst==NULL)
lst = init_intlist(i);
else
insert_intlist( lst, i);
}
}
fclose(ptr);
system("PAUSE");
return 0;
}
这里是 intlist.h 供任何可能需要的人使用:
#ifndef __intlist_h__
#define __intlist_h__
/* each entry in the list contains an int */
typedef struct intlist {
int datum;
struct intlist *next;
} INTLIST;
INTLIST *init_intlist( int n ); /* initializes the intlist with initial datum n */
int insert_intlist( INTLIST *lst, int n ); /* Inserts an int (n) into an intlist from the beginning*/
void list_append(INTLIST *list, void *datum); /* Inserts entry to the end of the list */
INTLIST* list_front(INTLIST *list); /*return the element at the front of the list, and remove it
from the list*/
void list_map( INTLIST *list, void (*f)(void *) ); /*Applies a function to each element of the list */
void list_delete( INTLIST *list ); /* Deletes (and frees) all entries in the list */
#endif
【问题讨论】:
-
你的教授给的原型是标题吗?
int insert_intlist( INTLIST *lst, int n );带有 Inserts an int (n) into an intlist from the beginning 的评论是错误的:使用原型,这是无法做到的。详情见我的回答。 -
@Alok:你不能自然而然地做到这一点。 Jerry 明白了,将新节点放在第二个,然后交换有效负载。我没有仔细阅读并假设在后面添加。
-
@dmckee:啊哈!我喜欢这种规则的弯曲——尽管我想知道这是否是教练想要的。另一种可能性是返回值是
int,所以该函数可能只是返回旧头部的有效负载,并将其替换为新值!这确实是一个简单的功能。