链表增加节点

caoxingyu

关注

阅读 34

2022-01-09

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)

0 0 举报