我是靠谱客的博主 寒冷鱼,这篇文章主要介绍链表的创建,现在分享给大家,希望可以做个参考。

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;
}

最后

以上就是寒冷鱼最近收集整理的关于链表的创建的全部内容,更多相关链表内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部