Data Structure _ Linear Table _ Chain Storage _ two-way cyclic linked list basic operation

Source: Internet
Author: User

Two-way linked list, the head node and the tail node link together, constitute a two-way circular chain list
The two-way circular linked list points the precursor pointer of the head node to the tail node, and points the trailing node's stamina pointer to the head node.
Empty table, the precursor of the head node and the subsequent pointers all point to themselves, this is to determine whether the two-way circular linked list is empty conditions,
Two-way cyclic linked list with symmetry
The disadvantage is to pay the cost of space

A doubly linked list is also called a doubly linked list, which is a list of two pointers in each data node, pointing directly to successive and direct precursors respectively. So, starting from any node in a doubly linked list, it is easy to access its predecessor and successor nodes. In general, we construct two-way circular linked lists.

Code

#pragmaMark defines a node type typedef int ELEMTYPE;TYPEDEF struct dulnode{elemtypeData; struct Dulnode*Prior//point to the precursor nodestruct Dulnode*Next//point to subsequent nodes}dulnode,*Dulinklist;#pragmaThe basic operation of the two-way loop linked list of Mark's lead node#pragmaMark---1.Initializing an empty bidirectional loop listvoidInitlist (dulinklist*L) {/ * Generate empty two-way loop linked list L * /    *L=(dulinklist) malloc (sizeof (Dulnode));if(*L) {(*L -Next=*L (*L -Prior=*L }ElseExit0);}#pragmaMark---2.Destroying a two-way circular link listvoidDestroylist (Dulinklist L) {dulinklist q,p=L -Next/ * p points to the first node * /     while(p!=L/ * p not to table header * /{Q=P -Next        Free (p); P=Q    } free (L); L=NULL;}#pragmaMark---2.Empty the two-way loop linked listvoidClearlist (Dulinklist L)/ * Do not change L * /{Dulinklist q,p=L -Next/ * p points to the first node * /     while(p!=L/ * p not to table header * /{Q=P -Next        Free (p); P=Q } L -Next=L -Prior=L/ * The two pointer fields of the head node point to themselves * /}#pragmaMark---3Verify that the table is empty table int listempty (dulinklist L) {/* Initial condition: Linear table L already exists */   if(L -Next==L&&L -Prior==Lreturn 1;Else   return 0;}#pragmaMark counts the number of elements int listlength (Dulinklist L) {/* Initial condition: l already exists. Operation Result: */int I=0; Dulinklist P=L -Next/ * p points to the first node * /     while(p!=L/ * p not to table header * /{i++; P=P -Next }returni;}#pragmaMark Assignment Status Getelem (dulinklist l,int I,elemtype*E) {/ * When the first element is present, its value is assigned to E and returns OK, otherwise the error is returned * /Int J=1;/ * J for Counter * /Dulinklist P=L -Next/ * p points to the first node * /     while(p!=L&&J<i) {p=P -Next J++; }if(p==L||J>I/ * element I does not exist * /        returnERROR;*E=P -Data;/ * Take the first element * /    returnOK;}//Find element precursorStatus Priorelem (dulinklist l,elemtype Cur_e,elemtype*Pre_e) {/* Operation result: If Cur_e is the data element of L and is not the first one, then return it with Pre_e, */    / * Otherwise the operation fails, pre_e undefined * /Dulinklist P=L -Next -Next/ * p points to the 2nd element * /     while(p!=L/ * p not to table header * /{if(p -Data==Cur_e) {*Pre_e=P -Prior -Data;returnOK; } p=P -Next }returnERROR;}//Find element successorStatus Nextelem (dulinklist l,elemtype Cur_e,elemtype*Next_e) {/* Operation result: If Cur_e is the data element of L and is not the last one, then return it with Next_e, */    / * Otherwise the operation fails, next_e undefined * /Dulinklist P=L -Next -Next/ * p points to the 2nd element * /     while(p!=L/ * p not to table header * /{if(p -Prior -Data==Cur_e) {*Next_e=P -Data;returnOK; } p=P -Next }returnERROR;}//Find element addressDulinklist getelemp (dulinklist l,int i)/ * plus * /{/ * Returns the address of the element I in the doubly linked list L. I is 0, which returns the address of the head node. If the first element does not exist, * /    / * Returns NULL * /Int J; Dulinklist P=L/ * p points to the head node * /    if(I<0||I>Listlength (L))/ * I value is illegal * /        return NULL; For (j=1; j<=I;j++) p=P -NextreturnP;}//insertion of elementsStatus Listinsert (dulinklist l,int i,elemtype e) {/ * Insert Element e,i before the first position in the double chain loop linear table L of the lead junction, the legal value of the 1≤i≤ table is +1 * /    / * Improve algorithm 2.18, otherwise you cannot insert an element before the +1 nodes in the table * /Dulinklist P,s;if(I<1||I>Listlength (L)+1)/ * I value is illegal * /        returnERROR; P=Getelemp (l,i-1);/ * Determine the position of the precursor of element I in L p * /    if(!P/* P=null, where the precursor of element I is absent (the precursor of the 1th element is the head node) */        returnERROR; S=(dulinklist) malloc (sizeof (Dulnode));if(!s) exit (0); S -Data=E S -Prior=P/ * Insert after element i-1 * /S -Next=P -Next P -Next -Prior=S P -Next=SreturnOK;} Status listdelete (dulinklist l,int I,elemtype*E) {/ * Delete the first element of the double chain loop linear table L of the lead node, the legal value for I is 1≤i≤ table length * /Dulinklist p;if(I<1)/ * I value is illegal * /        returnERROR; P=Getelemp (L,i);/ * Position pointer p */for element I in L    if(!P/ * P=null, that is, the first element does not exist * /        returnERROR;*E=P -Data; P -Prior -Next=P -Next P -Next -Prior=P -Prior Free (p);returnOK;}voidVisit (elemtype c) {printf ("%d", c);}

Call

intMainintargcConst Char* argv[]) {//Insert code here ...Dulinklist L; Elemtype e;intJ    Status i; Initlist (&AMP;L);/ * Initialize single-loop linked list L * /Listinsert (L,1, the); Listinsert (L,2, -); Listinsert (L,3, *); Listinsert (L,4, $); J=listlength (L);printf("Number of data elements in L =%d\n", j);    Listtraverse (L,visit); Priorelem (L, -, &e);/ * Seek the precursor of element 5 * /    printf("25 The value of the preceding element is%d. \ n ", e); Nextelem (L, *, &e);/ * For the successor of Element 3 * /    printf(The value of the element after 35 is%d. \ n ", e);printf("L is empty%d (1: Empty 0: NO) \ n", Listempty (L));//Delete second elementListdelete (L,2, &e); J=listlength (L);printf("Number of data elements in L =%d\n", j); Listtraverse (L,visit);return 0;}

Run results

Data Structure _ Linear Table _ Chain Storage _ two-way cyclic linked list basic operation

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.