【发布时间】:2013-10-09 14:12:55
【问题描述】:
在头文件中:
struct myStruct{
int data;
struct myStruct *next;
};
typedef struct myStruct myStruct;
相对函数:
myStruct * create(){
myStruct * a = NULL;
int size;
printf("Enter Size of List : ");
scanf("%d",&size);
for(int i = 0;i<size;i++){
/*
* can't seem to figure out how to do this correctly.
*
* I know I have to use malloc(sizeof()),etc..
*
* I've only had success with creating the list backwards.
*
* In this loop there would be a scan from user input for
* the data instance
*/
}
return a;
}
所以我认为这很简单。任何帮助将不胜感激。
【问题讨论】:
-
这个 'myStruct a = NULL' 是否编译?如果是这样,我很惊讶。
-
对不起,我是凭记忆做的(不是复制粘贴)。 'a' 是一个指针...已编辑
-
在您的循环中,向用户询问号码。用 malloc 分配一个 myStruct 并将数据字段设置为来自用户的数字。跟踪列表中的最新项目并使用它来设置下一个指针。然后将最近的项目设置为您刚刚分配的项目。祝你好运。
-
有道理(开始实施)...感谢您的帮助
-
另一种可能性是向后创建列表,然后在完成后将其反转。这种技术在函数式语言中很常见,在创建列表节点后不允许对其进行变异。
标签: c dynamic linked-list