学完C语言有好长时间了,我觉得应该把C中一些常用的函数总结一下,这即是对知识的恐固,也能在其中发现自己的不足.
以下代码实现创建动态简单链表功能:
#include
#define LEN sizeof(struct employeeData)
struct employeeData
{
int id;
struct employeeData *next;
};
struct employeeData *create(int);
void main()
{
int n,i=1;
struct employeeData *startPtr,*ptr;
printf("请输入要创建的链表结点数\n");
scanf("%d",&n);
startPtr = create(n);
ptr = startPtr;
do
{
printf("第 %3d 个结点中的值 %6d\n",i,ptr->id);
ptr = ptr->next;
i++;
}while (ptr != NULL);
}
struct employeeData *create(int m)
{
struct employeeData *head,*ptr;
int i;
for (i = 1; i <= m; i++)
{
ptr = (struct employeeData *) malloc(LEN);
if (ptr == NULL)
{printf("无法分配内存\n");return(NULL);}
else
{
ptr->id = i;
if (i == 1)
{
ptr->next = NULL;
head = ptr;
}
else
{
ptr->next = head;
head = ptr;
}
}
}
return (head);
}
阅读(931) | 评论(0) | 转发(0) |