[] Data structure to create a single list of 9

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zztingfeng/article/details/90343130

The method of creating a single linked list in two ways: the head and tail interpolation interpolation.        
Before the first interpolation method is to add a node is inserted into the first node, a schematic diagram is as follows:

After the end of the interpolation is a new node is inserted into the last node, the following schematic:

code segment

//用头插法创建带头结点的单链表
    void LinkedListCreateHeadL(LinkedList L, ElemType a[n])
    {
      L=(LNode *)malloc(sizeof(LNode));
      if(L==NULL)
      {
        printf("申请空间失败!");
        exit(0);
      }
      L->next=NULL;

      for(i=0; i<n; i++)
      {
        p=(LNode *)malloc(sizeof(LNode));
        if(p==NULL)
        {
          printf("申请空间失败!");
          exit(0);
        }

        p->data=a[i];
        p->next=L->next;
        L->next=p;
      }
    }
    //用尾插法创建带头结点的单链表
    void LinkedListCreateTailL(LinkedList L, ElemType a[n])
    {
      L=(LNode *)malloc(sizeof(LNode));
      if(L==NULL)
      {
        printf("申请空间失败!");
        exit(0);
      }
      L->next=NULL;
      tail=L;         //设置尾指针,方便插入

      for(j=0; j<n; j++)
      {
        p=(LNode *)malloc(sizeof(LNode));
        if(p==NULL)
        {
          printf("申请空间失败!");
          exit(0);
        }

        p->data=a[j];
        p->next=NULL;
        tail->next=p;
        tail=p;
      }
    }

 

Guess you like

Origin blog.csdn.net/zztingfeng/article/details/90343130