【发布时间】:2019-04-09 10:59:15
【问题描述】:
在字符串链表中保存一个节点。
struct Node
{
char* namePtr_;
struct Node* nextPtr_;
};
struct Node* makeList(int argc,
char* argv[])
{
struct Node* list = NULL;
struct Node* end = NULL;
int i;
创建并返回从 'argv[1]' 到 'argv[argc-1]' 的字符串链表,或者在 'argc'
for (i = 1; i < argc; i++)
{
struct Node * ptrNode;
ptrNode = (struct Node*)malloc(sizeof(*list));
ptrNode -> namePtr_ = (char *) malloc(strlen(argv[i])+1);
strcpy(ptrNode -> namePtr_, argv[i]);
ptrNode -> nextPtr_ = NULL;
list = ptrNode; //I think my problem is here
}
}
return(list);
}
打印 'list' 中的 'namePtr_ 值。
void print (const struct Node* list)
{
const struct Node* run;
run = list;
while(run != NULL){
printf("%s\n", run->namePtr_);
run = run -> nextPtr_;
}
}
释放列表的 nextPtr 和 namePtr 以及所有 nextPtr_ 后继者。
void release (struct Node* list)
{
struct Node* ans = list;
free(ans);
}
创建、打印和释放()一个链表。
int main(int argc, char* argv[])
{
struct Node* list;
list = makeList(argc,argv);
print(list);
release(list);
return(EXIT_SUCCESS);
}
应该输出:
./argList hello there
hello
there
./argList hello there everyone
hello
there
everyone
但我的输出是:
./argList hello there
there
./argList hello there everyone
everyone
【问题讨论】:
标签: c linked-list