Linear table-linked list (iii)

Source: Internet
Author: User

One, single linked list basic operation

(1) Clear single-linked list

while(L->next){   p=L->next;   L->next=p->next;   free(p);  }

Analysis: P points to the next field of L, and the next field of L is in the next field (P=l->next) that points to p, which is actually the next field where L points to L. Then empty P
Keep looping, know l->next = = NULL
(2) destruction of single-linked list

while(L){     p=L->next;     free(L);     L=p;}

(3) Single-linked list to determine whether it is empty

if(L->next==NULL)        returnTRUE;else     returnFALSE;

(4) Table length for single linked list

int ListLength(LinkList L){     p=L->next;    i=0;    while(p){        i++;        p=p->next;    }    return i;}

(5) How to find elements in a single linked list

 p=L->next; i=1while&& p->data!=e){    p=p->next; i++; }  if(p){     return i;  }       else{     return0;  }     

Analysis: Start the search from the first node, search succeeds, return the bit order; otherwise, return 0

Second, reverse order to establish a single linked list

Analysis:

    1. Create an empty single-linked list of lead nodes
    2. Enter the data element AI, create a new node p, and insert p after the head node to become the first node.
    3. Repeat the 2nd step until you complete the creation of the single-linked list.
      The code is as follows:
voidCreatelist_n (linklist&L, int n) {//Reverse input n data elements, set up a single linked list of leading nodesL=(linklist) malloc (sizeof (Lnode)); L -Next= NULL; for (i= 1; I<=N I++) {P=(linklist) malloc (sizeof (Lnode)); scanf&P -Data);//INPUT element valueP -Next=L -Next L -Next=P//Insert}
Third, sequential establishment of single-linked list

Analysis:

    1. Create an empty single-linked list of lead nodes
    2. Enter the data element AI, create a new node, and insert it into the last node after the tail node p.
    3. Repeat the 2nd step until you complete the creation of the single-linked list
      The code is as follows:
L = (LinkList) malloc (sizeof (LNode));LNULL;    p=L;for(i=1;i<=n;i++){    q=(LinkList)malloc(sizeof(LNode));            scanf(&q->data);            q->next=p->next;            p->next=q;            p=q;}

Linear table-linked list (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.