概述
1.无头节点
struct stud_node *createlist()
{
struct stud_node *tail,*p,*head;
head = (struct stud_node *)malloc(sizeof(struct stud_node));
tail = head = NULL;
int num;
scanf("%d",&num);
p = (struct stud_node *)malloc(sizeof(struct stud_node));
while(num!=0)
{
p->num = num;
scanf("%s%d",p->name,&p->score);
if(head==NULL)
{
head = p;
head->next = NULL;
}
if(tail!=NULL)
{
tail->next = p;
}
tail = p;
tail->next = NULL;
p = (struct stud_node *)malloc(sizeof(struct stud_node));
scanf("%d",&num);
}
return head;
}
2.含有头节点
struct stud_node *createlist()
{
struct stud_node *tail,*p,*head;
head = (struct stud_node *)malloc(sizeof(struct stud_node));
tail = head = NULL;
int num;
scanf("%d",&num);
p = (struct stud_node *)malloc(sizeof(struct stud_node));
while(num!=0)
{
p->num = num;
scanf("%s%d",p->name,&p->score);
tail->next = p;
tail = p;
tail->next = NULL;
p = (struct stud_node *)malloc(sizeof(struct stud_node));
scanf("%d",&num);
}
return head->next;
}
3.头插法
struct ListNode *createlist()
{
struct ListNode *head,*p;
head = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next = NULL;
int data;
scanf("%d",&data);
while(~data)
{
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = data;
p->next = head->next;
head->next = p;
scanf("%d",&data);
}
return head->next;
}
最后
以上就是寒冷鱼为你收集整理的链表的创建的全部内容,希望文章能够帮你解决链表的创建所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复