C 这是否正确实现了链接列表的初始化列表功能?
无论我在哪里查看链表实现,我都会发现当我初始化一个列表时,我的代码应该是这样的:C 这是否正确实现了链接列表的初始化列表功能?,c,struct,linked-list,C,Struct,Linked List,无论我在哪里查看链表实现,我都会发现当我初始化一个列表时,我的代码应该是这样的: typedef struct node { int val; struct node * next; } node_t; 但我的任务是为我的initialize函数编写一个函数initializeList,它创建一个员工信息的链表,其中每个节点都包含一个employeeData结构和一个名为next的指针 我从中得到的是这样做,但我认为这是不对的: #include <stdio.h> #includ
typedef struct node {
int val;
struct node * next;
} node_t;
但我的任务是为我的initialize函数编写一个函数initializeList,它创建一个员工信息的链表,其中每个节点都包含一个employeeData结构和一个名为next的指针
我从中得到的是这样做,但我认为这是不对的:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
FILE * data;
typedef struct{ // Main struct containing needed data types
int EMP_ID;
int dep;
int rank;
double salary;
char name[20];
} node;
struct node { node employeeData; struct node *next; }; // struct node
struct node *head, *z, *t;
initializeList ( node employeeData, FILE *data ) // initialize list function
{
head = ( node * ) malloc( sizeof *head );
z = ( node * ) malloc( sizeof *z );
head->next = z;
z->next = z;
}
int main (void)
{
data = fopen( "empInfo.txt","r" );
node employeeData = {0,0,0,0,0};
while (getchar () != '\n')
getchar ();
fclose( data );
return 0;
}
这是正确的实施方式吗?非常新的编程,所以请对我放松。我的任务还没有完成,所以我知道现在它实际上什么都没做 您有两个名为node的结构,应该重命名其中一个。head=node*malloc sizeof*head;:节点*应为结构节点*或省略。此外,您的代码在整个过程中什么也不做。initializeList应该有一个显式返回类型void,并且需要获取指向节点的指针,而不是节点的副本。否则,您无法将信息/更改返回到调用代码。它不使用数据参数。每次调用它时,它都会践踏head全局变量,这不太可能带来快乐。谢谢大家!我如何接受其中一个答案?你还没有答案。