嵌入式-头插法插入节点

复制代码
#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 * InsertFromHead(struct Test * head)
{
  struct Test * new;
  new=(struct Test*)malloc(sizeof(struct Test));
  printf("input your data\n");
  scanf("%d",&(new->data));
  if(head==NULL)
  {
     head=new;
     return head;
  }
  else
  {
     new->next=head;
     head=new;
  }
  return head;
}


int main()
{
   struct Test * head=NULL;
   head=InsertFromHead(head);
   head=InsertFromHead(head);
   head=InsertFromHead(head);
   printf("after insert data\n");
   printfLink(head);
   return 0;
}
复制代码

输出结果:

input your data
1
input your data
2
input your data
3
after insert data
3
2
1

posted @   WellMandala  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示