Thoroughly understand why the list uses a two-level pointer or a first-level pointer reference __ data structure and algorithm

Source: Internet
Author: User

Reprint: http://blog.csdn.net/u012434102/article/details/44886339

When you write data structures with C + +, the link list and two-fork tree often need to use a two-level pointer or a pointer to a reference, then when to use when not.
First look at a simple C + + list operating procedures:

 #include "stdio.h" #include "stdlib.h" #include "time.h" #define OK 1 #define ERROR 0 #define TRUE 1  #define FALSE 0 #define MAXSIZE 20/* Storage space initial allocation/typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK/typedef  
    The int elemtype;/* elemtype type is based on the actual situation, which is assumed to be int */Status visit (elemtype c) {printf ("%d", c);  
return OK;  
    typedef struct NODE {elemtype data;  
struct Node *next;  
}node; typedef struct NODE *linklist; /* Define LINKLIST///Initialize header, use first level pointer (this method is invalid) Status InitList1 (linklist L)//equivalent to node *l {l= (linklist) malloc (size Of (Node)); /* Generate head node, and make L point to this head node * * * IF (!  
    L)/* Storage allocation failure/return ERROR; l->next=null;  
/* Pointer field is empty/return OK; ///Initialize header, with level two pointer Status InitList2 (linklist *l)//equivalent to Node **l {*l= (linklist) malloc (sizeof (node));/* Generating Header node, and make L point to this head node/if (!  
    *L)/* Storage allocation failure/return ERROR; (*l)->next=null;  
/* Pointer field is empty/return OK;  

}Initializes the header with a level pointer referencing the Status InitList3 (linklist &l)//equivalent to Node *&l {l= (linklist) malloc (sizeof (node));/* Generated Head node and make L point to this head node * * * IF (!  
    L)/* Storage allocation failure/return ERROR; l->next=null;  
/* Pointer field is empty/return OK;  
    }//Empty list, use level two pointer Status ClearList1 (linklist *l) {linklist p,q;           p= (*l)->next;  
        /* p points to the first node/while (p)/* not to the footer/{q=p->next;  
        Free (p);  
    p=q;        } (*l)->next=null;  
/* head node pointer field is empty/return OK;  
    //Empty the list, using the first level pointer Status ClearList2 (linklist L) {linklist p,q;           p=l->next;  
        /* p points to the first node/while (p)/* not to the footer/{q=p->next;  
        Free (p);  
    p=q;        } l->next=null;  
/* head node pointer field is empty/return OK;  
    }//Destroy linked list, use first level pointer (this method is invalid) Status DestroyList1 (linklist L) {linklist p,q;           p=l->next; /* p points to the first node * *(p)/* not to the end of the table * * q=p->next;  
        Free (p);  
    p=q;  
    Free (L);  
    L=null;  
return OK;  
    }//Destroy linked list, use level two pointer Status DestroyList2 (linklist *l) {linklist p,q;           p= (*l)->next;  
        /* p points to the first node/while (p)/* not to the footer/{q=p->next;  
        Free (p);  
    p=q;  
    Free (*l);  
    *l=null;  
return OK;  
    }//Destroy linked list, use the first level pointer to reference Status DESTROYLIST3 (linklist &l) {linklist p,q;           p=l->next;  
        /* p points to the first node/while (p)/* not to the footer/{q=p->next;  
        Free (p);  
    p=q;  
    Free (L);  
    L=null;  
return OK;   
    }/* Initial condition: sequential linear table L already exist, 1≤i≤listlength (L)////////////////////////////Status Getelem (linklist l,int i,elemtype *e) {  
    Int J;     Linklist p;     /* Declare a node p * * * p = l->next;      /* Let P point to the first node of the linked list L/j = 1; /* J for counter */While(P && j<i)/* p is not null or the counter J is not equal to I, the loop continues * * p = p->next;  
    /* Let P point to the next node * * ++J;  } if (!p | | j>i) return ERROR;   /* I element does not exist */*e = p->data;  
* * Take the first element of the data/return OK;  
    ///Insert element in the middle, with level two pointer Status ListInsert1 (linklist *l,int i,elemtype e) {int J;  
    Linklist p,s;     
    p = *l;  
    j = 1;  
        while (P && J < i) * * Find the first node/{p = p->next;  
    ++j;   } if (!p | | j > i) return ERROR;  /* I element does not exist/s = (linklist) malloc (sizeof (Node));    
    /* Generate new node (C language standard function) */s->data = e;      S->next = p->next;          /* Assign the subsequent node of P to the successor of S/p->next = s;  
/* Assign value to P's successor/return OK;  
    }//Insert element in the middle, with the first level pointer Status ListInsert2 (linklist l,int i,elemtype e) {int J;  
    Linklist p,s;     
    p = L;  
    j = 1;  
       while (P && J < i) * * Find the first node/{ p = p->next;  
    ++j;   } if (!p | | j > i) return ERROR;  /* I element does not exist/s = (linklist) malloc (sizeof (Node));    
    /* Generate new node (C language standard function) */s->data = e;      S->next = p->next;          /* Assign the subsequent node of P to the successor of S/p->next = s;  
/* Assign value to P's successor/return OK;  
    ///delete an element with the level two pointer Status ListDelete1 (linklist *l,int i,elemtype *e) {int J;  
    Linklist p,q;  
    p = *l;  
    j = 1;  
        while (P->next && J < i)/* traversal look for the first element/{p = p->next;  
    ++j; } if (! ( P->next) | |           J > I) return ERROR;  
    /* I element does not exist */q = p->next;            P->next = q->next;               /* Assign the successor of Q to P's successor/*e = q->data;                    * * to the Q node in the data to E/free (q);  
/* Let the system recycle this node, free memory/return OK;  
    ///delete an element with the first level pointer Status ListDelete2 (linklist l,int i,elemtype *e) {int J;  
    Linklist p,q;  
 p = L;   j = 1;  
        while (P->next && J < i)/* traversal look for the first element/{p = p->next;  
    ++j; } if (! ( P->next) | |           J > I) return ERROR;  
    /* I element does not exist */q = p->next;            P->next = q->next;               /* Assign the successor of Q to P's successor/*e = q->data;                    * * to the Q node in the data to E/free (q);  
/* Let the system recycle this node, free memory/return OK;  
        while (p) {visit (p->data);  
    p=p->next;  
    printf ("\ n");  
return OK;  
    int main () {linklist L;  
    Elemtype e;  
    Status i;  
    int j,k;   InitList1 (L);  First-level pointer method to create the table header, Failure//initlist2 (&AMP;L);     Second-level pointer method to create a table header, successful InitList3 (L);  
    First-level pointer reference to create a table header, success for (j=1;j<=7;j++) ListInsert2 (L,1,J);  
    printf ("First-level pointer is inserted after 1~7 in the header of L"); ListtrAverse (L);  
    ListInsert1 (&l,3,12);  
    printf ("Two-level pointer mode inserted in the middle of L 12 after:");   

    Listtraverse (L); ListInsert2 (L,

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.