嵌入式-链表尾插法插入

链表通过尾部插入

复制代码
#include<stdio.h>
#include<stdlib.h>

struct Test
{
  int data;
  struct Test * next;
};

printfLink(struct Test * head)
{
   while(head!=NULL)
   {
      printf("%d\n",head->data);
      head=head->next;
   }
   putchar('\n');
}

struct Test * InsertEndNode(struct Test* head,struct Test * new)
{  
   struct Test * p=head;
   if(head==NULL)
   {
      head=new;
      return head;
   }
   while(p->next!=NULL)
   {
      p=p->next;
   }
   p->next=new;
   return head;
}
int main()
{
   struct Test *head=(struct Test *)malloc(sizeof(struct Test));
   head->data=1;
   struct Test new1={2,NULL};
   struct Test new2={3,NULL};
   struct Test new3={4,NULL};
   struct Test new4={5,NULL};
   struct Test * hp1=InsertEndNode(head,&new1);
   struct Test * hp2=InsertEndNode(hp1,&new2);
   struct Test * hp3=InsertEndNode(hp2,&new3);
   printfLink(hp3);
   return 0;
}
复制代码

输出结果:

1
2
3
4

posted @   WellMandala  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示