单链表的建立(头插法和尾插法)

可供参考:

https://blog.csdn.net/qq_41028985/article/details/82859199

https://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100000800&idx=1&sn=7e5a3c63b697e1eb273c19d96a49af02&chksm=7e0d6ae7497ae3f1d8dfb51cc7aa7192e5919864f3c10af553f5fa507d62193fb681c5f36944#rd%C2%A0

头插入是指在链表的表头节点之后(记得是表头),之前插入的部分被顶到了后面

void CreateListHead(LinkList *L, int n)
{
  LinkList p;
  int i;

  srand(time(0));

  *L = (LinkList)malloc(sizeof(Node));
  (*L)->next = NULL;

  for(i=0; i < n; i++)
  {
    p->data = rand()%100+1;
    p->next = (*L)->next;//****先把表头next链接到新建立节点的next****
    (*L)->next = p;//****把表头的next指针指向新建立节点
  }
}


尾插法就是常用的插入方法,比较容易理解。

void  CreateListtail(Listlist *head,int n)

{

   int *p,*end;

   Linklist node;

   srand(time(0));

   head = (Linklist)malloc(sizeof(Node));

   (*head)->next = NULL;

   for(int i = 0; i <=n-1;i++ )

   {

      node = (Linklist)malloc(sizeof(Node));

      node->data = rand()%100+1;

      end->next = node;

      end = node

}

猜你喜欢

转载自blog.csdn.net/ENAIC__suger/article/details/88074858