为什么我的线性链表不能插入节点数据也写不进去数据

屈工智能科技 2017-11-14 07:22:48
#include <stdio.h>
#include <stdlib.h>

//结构体类型
typedef struct snode
{
int name;
int asdf;
}link_node;

//链表结点类型
typedef struct node
{
link_node data;
struct node *next;
}linknode;


//创建链表
linknode *create_empty_linklist()
{
linknode *head = NULL;
head = (linknode *)malloc(sizeof(linknode));
head->next = NULL;
return head;
}

//判断链表是否为空
int is_empty_linlist(linknode *head)
{
return head->next == NULL;
}

//头插法,在头节点之后插入
int insert_head_liklist(linknode *head,int name,int asdf)
{
linknode *temp = NULL;
temp = (linknode *)malloc(sizeof(linknode));
temp->data.name = name;
temp->data.asdf = asdf;
temp->next = head->next;
return 0;
}

//链表输出
int print_linklist(linknode *head)
{
linknode *p = head->next;
while(p)
{
printf("%d%d",p->data.name,p->data.asdf);
p = p->next;//找到尾0->next
}
printf("\n");
return 0;
}

//删除节点
int delete_assign_linklist(linknode *head,int data)
{
linknode *temp = NULL;
linknode *p = head;

temp = (linknode *)malloc(sizeof(linknode));

while(p->next)
{
if (p->next->data.name == data)
{
break;
}
p = p->next;
}
temp = p->next;
p->next = temp->next;

free(temp);
temp = NULL;
return 0;
}

//主函数
void main()
{
linknode *head = create_empty_linklist();
//插入三个节点
insert_head_liklist(head,1,2);
insert_head_liklist(head,2,4);
insert_head_liklist(head,5,6);
print_linklist(head);
system("pause");
}
...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2017-11-14
  • 打赏
  • 举报
回复
你还需要让head->next指向你添加的temp
真相重于对错 2017-11-14
  • 打赏
  • 举报
回复
int insert_head_liklist(linknode *head,int name,int asdf) { linknode *temp = NULL; temp = (linknode *)malloc(sizeof(linknode)); temp->data.name = name; temp->data.asdf = asdf; temp->next = head->next; head->next = temp; return 0; }

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧