Linear chain List of lead nodes in practical application

Source: Internet
Author: User

/*======== the linear list type of the lead node =========*/typedef char elemtype//node type typedef struct lnode{char data; struct Lnode *next;} *link,*position;    List type typedef struct{link head,tail; int Len;}  linklist; /*======================================================*//*======= Some functions that are called in other function definitions ===============*//*=========  =============================================*//*---Compare---compare the size relationship of two elements */int compare (char A,char b) {return a-A;}        /*---Visit---*/int visit (Link p) {if (...)    return 1;     else return 0;    }/*---length---to find the lengths of the chain */int length (link s) {int i=0;    Link P=null;    P=s;        while (p->next!=null) {p=p->next;    i++; } return i;}    /*---Print---all elements of the list are printed on the screen */void print (linklist L) {link p=null;    P=l.head;        if (!p->next) {printf ("\nthe linklist is empty.\n\n");    return;    } printf ("The List:");        while (p) {printf ("%c-", p->data);    p=p->next; }} /*======================================================*//*========== definition of a function that operates on a single-chain linear table with a leading node ==*//*===============================================    =======*//*---Assigns a node that is pointed to by P and assigns the value e---*/position makenode (elemtype e) {Link p=null;    p= (Lnode) malloc (sizeof (struct));        if (p) {p->data=e;    p->next=null;    } else return; return p;}  /*---Release P points to the node-*/void freenode (link p) {free (p);} /*---Constructs an empty linear table-*/void initlist (linklist *l) {l->head=makenode (' l ') pointed by L;//Generate Header node l->head->next=null;/*    Not l->head=null*/l->tail=l->head;. l->len=0;}    /*----------Destroy the linear table pointed by L---------*/void destroylist (linklist *l) {Link p;    P= (*l). Tail;        while (p!= (*l). Head) {p=priorpos (*l,p);    Freenode (P->next); } freenode ((*l). Head);}    /* Place the Linear table L as an empty table and release the head node of the original list */void clearlist (linklist *l) {link p;    P= (*l). Tail;        while (p!= (*l). Head) {p=priorpos (*l,p);    Freenode (P->next); } freenode ((*l). Head);} /*---Insert the node that s points toLinear list before the first node-*/void Insfirst (linklist *l,link s) {s->next=l->head->next; if (!       L->head->next) l->tail=s;    /* When performing this operation to an empty linear table */l->head->next=s; l->len++;} /*---Delete the first node in the table and return with Q-*/void delfirst (linklist *l,link q) {if (! L->head->next) {printf ("\nthe linklist is empty,can not delete:        \ n ");    return 0;    } q=l->head->next; L->head->next=l->head->next->next;}    /*---Link a string of nodes in the pointer s to the last node of the linear table L-*/void Append (linklist *l,link s) {link q;    q=l->head; if (!        L->tail) {/* take into account that the list is empty */l->head->next=s;    while (q->next) q=q->next;/* tail node processing */l->tail=q;    } l->tail->next=q=s;    while (Q->next) q=q->next;/* can not forget the processing of the tail node */l->tail=q; L->len+=length (s);} /*---Delete the tail node in the linear table L-*/void Remove (linklist *l,link q) {if (! L->tail) {printf ("\nthe linklist is Empty,can not remonde..        \ n "); return 0;                          } q=l->tail;    L->tail=priorpos (*L,Q); L->tail->next=null;}    /*----*/void insbefore (linklist *l,link p,link s) {link q) before inserting the node pointed by S into the node indicated by P;    Q=priorpos (*L,P);    s->next=p; Q->next=s;}    /*----*/void insafter (linklist *l,link p,link s) after inserting the points pointed to by P into the node indicated by the point "{s->next=p->next; P->next=s;} /*---Use e to update the current node-*/void setcurelem (link p,elemtype e) {p->data=e;}/*---Return the value of the element in the node referred to in P-*/elemtype Getcurele  M (Link p) {return p->data;}    int Listempty (linklist L) {/*---returns 1 if the linear table is empty table, otherwise returns 0-*/if (L.head==l.tail) return 1;  return 0;}  int Listlength (linklist L) {/*---Returns the number of elements in the linear table-*/return l.len;}  Position GetHead (linklist l) {/*---Returns the position of the head node in the linear table L-*/return l.head;}  Position GetLast (linklist l) {/*-----Returns the position of the last node in the linear table l-------*/return l.tail;    /*---Returns the position of the direct precursor of the node referred to in P-*/position priorpos (linklist l,link p) {Link q;    Q=l.head; if (q->next==p) return0;    while (q->next!=p) q=q->next; return q;}    /*-----Returns the position of the direct successor of the point indicated by P-------*/position nextpos (link p) {link q;    q=p->next; return q;}    /*-----Return the position of the first node in the linear table L with P and return to OK-------*/void Locatepos (linklist l,int i,link p) {p=l.head; if (i<=0| |    I>listlength (L)) return 0;        while (i) {p=p->next;    i--;    }/*----Returns the first node order position in the table that satisfies a certain function relationship with E----*/int Locatelem (linklist l,elemtype e) {int i=0;    Link p;    p=l.head->next;        while (compare (p->data,e) &&p) {p=p->next;    ++i;        if (!p) {/* takes into account the case where the specified element is not found */printf ("\nthere ' s no '%c ' in this linklist.", e);    return 0; } return i;}    /*----Use a function to traverse all nodes in the table-------*/void listtraverse (linklist L) {Link p;    P=l.head;   while (!visit (p)) p=p->next; }



Arranges elements of a single-stranded linear table, LA and lb, by value non-descending


Status mergelist_l (nlinklist &la, Nlinklist &lb, nlinklist &lc, int (*compare) (Elemtype, El    Emtype)) {//algorithm 2.21//The elements of the single-chain linear table LA and LB are known to be non-descending by value.    The elements that merge LA and lb to get new single-stranded linear table LC,LC are also non-descending by value.    Nlink ha, HB;    Position PA, Pb, q;    Elemtype A, B; if (!  Initlist (Lc)) return ERROR;      Storage space allocation Failure ha = GetHead (La);    Ha and HB point to the head node of LA and Lb respectively HB = gethead (lb);  PA = Nextpos (La, ha);    PA and PB respectively point to LA and Lb in the current node PB = Nextpos (Lb, HB);  while (PA && pb) {//LA and LB are non-null a = Getcurelem (PA);        A and B are the current comparison elements in two tables B = Getcurelem (PB);  if ((*compare) (A, b) <=0) {//A≤b Delfirst (ha, q);  Append (Lc, q);          PA = Nextpos (La, PA);  } else {//A>b Delfirst (HB, q);  Append (Lc, q);          PB = Nextpos (Lb, PB);        }}//While if (PA) Append (Lc, PA);        Link La remaining nodes else Append (Lc, PB);       Link the remaining node freenode (ha) in lb;      Freenode (HB);        Release the head node of LA and LB return OK;} mergelist_l


Linear chain List of lead nodes in practical application

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.