0
点赞
收藏
分享

微信扫一扫

链表增加节点

caoxingyu 2022-01-09 阅读 34
struct node *add (struct node *head, int n)
{
      struct node *p,*end;
      p=(struct node*)malloc(sizeof(struct node));
      p->data=n;
      p->next=NULL;
      end=head;
      if(end!=NULL)//不是空链表
        {
           while(end->next!=NULL)
            {
                end=end->next;//移动到链表尾部
            }
          end->next=p;//将节点添加至链表尾部
        }
      else
        {
            head=p;//如果为空链表,直接将节点放头部
        }
return head;
}
举报

相关推荐

0 条评论