Linear tables-04. Chain Storage structure (bidirectional loop linked list)

Source: Internet
Author: User

The following is a linear table implemented with a bidirectional cyclic link list

#include <stdio.h> #include <stdlib.h> #include <time.h> #define OK 1#define ERROR 0#define TRUE Define FALSE 0typedef int Elemtype;//elemtype Here is assumed to be int, can be changed as needed typedef int STATUS;//STATUS is the type of function, whose value is the function result status code, such as OK and other typedef struct DULNODE//linear table of the bidirectional list storage structure {elemtype data;struct dulnode *prior;//Direct precursor pointer struct Dulnode *next;//direct successor} dulnode;/* Initializes a bidirectional loop linked list */status initlist (Dulnode **l) {*l= (Dulnode *) malloc (sizeof (Dulnode)); *l)) return ERROR, (*l)->next= (*l)->prior=*l;return OK;} /* Initial condition: Chain linear table L already exists *//** operation result: Returns the number of data elements in L */int listlength (Dulnode *l) {int I=0;dulnode *p=l->next;while (p!=l) {p=p- >next;i++;} return i;} /* Initial conditions: bidirectional cyclic link list L already exists, 1≤i≤listlength (l) +1*//* operation result: Insert new data element in L position e,l length plus 1*/status listinsert (dulnode *l,int I, Elemtype e) {int J;dulnode *p,*s;//p points to the previous node to be inserted, S is the new node if (i<1)//The insertion position is unreasonable return error;p=l;j=1;while (j<i)/* Find the first node of the I node */{p=p->next;j++;if (p==l)//not found return ERROR;} s= (Dulnode *) malloc (sizeof (Dulnode)); S->data=e;s->prior=p;s->next=p->next;p->next->prior=s;p->next=s;return OK;} /* Initial conditions: bidirectional cyclic link list L already exists//* operation result: Each data element of output L */status listtraverse (Dulnode *l) {dulnode *p=l->next;while (p!=l) {printf ("%d ", P->data);p =p->next;} printf ("\ n"); return OK;} /* Initial conditions: Two-way circular linked list L already exists. /* Operation Result: Returns True if L is a blank table, otherwise false */status Listempty (Dulnode *l) {if (l->next==l && l->prior==l) return True    ; else return FALSE;} /* Initial conditions: Two-way circular linked list L already exists. *//* operation Result: Resets L to empty table. */status clearlist (Dulnode *l) {Dulnode *p;//p points to the first node P=l->next;while (p!=l) {p=p->next;free (p->prior);} The two pointer fields of the l->next=l->prior=l;//head node point to their own return OK; /* Initial conditions: bidirectional cyclic link list L already exists, 1≤i≤listlength (l) *//* operation result: Returns the value of the I data element in L with E */status getelem (dulnode *l,int i,elemtype *e) {Dulnode * P;int j;if (i<1)//value position unreasonable return error;p=l->next;//p point to the first node J=1;while (p!=l&&j<i) {p=p->next;j++;} if (p==l)//First Data element does not exist return Error;*e=p->data;return OK;} /* Initial condition: bidirectional cyclic link list L already exists *//* operation result: Returns the bit order of the 1th data element in L that satisfies the relationship with E.    *//* if such a data element does not exist, the return value is 0*/status Locateelem (Dulnode *l,elemtype e) {int i=0; Dulnode *p=l->nexT        while (p!=l) {i++;        if (p->data==e)/* Find such data element */return I;    p=p->next; } return 0;} /* Initial conditions: Two-way cyclic link list L already exists, 1≤i≤listlength (l) *//* operation Result: Delete the I data element of L, and return its value with E, */status listdelete (dulnode *l,int i,elemtype *e) { Dulnode *p;//p points to the I node int j;p=l->next;j=1;if (I&LT;1)//delete position unreasonable return Error;while (p!=l&&j<i) {p=p-> next;j++;} if (p==l)//First Data element does not exist return Error;*e=p->data;p->prior->next=p->next;p->next->prior=p->prior ; free (p); return OK;} /* Initial conditions: bidirectional cyclic link list L already exists//* operation result: Returns the value of the first Data element value of Cur_e in L pre_e*/status Priorelem (dulnode *l,elemtype cur_e,elemtype *pre_ e) {Dulnode *p;p=l->next;while (p!=l) {if (p->data==cur_e)//Find data element cur_e Node {if (p->prior==l)// If the node is the first node return Error;*pre_e=p->prior->data;return OK;} P=p->next;} return ERROR;} /* Initial conditions: bidirectional cyclic link list L already exists//* operation result: Returns the value of the cur_e of the first Data element value in L pre_e*/status Nextelem (dulnode *l,elemtype cur_e,elemtype *next_ e) {Dulnode *p;p=l->next;while (p!=l) {if (p->data==cur_e)//Find data element cur_e node {if(p->next==l)//If the node is the last node return Error;*next_e=p->next->data;return OK;} P=p->next;} return ERROR;} int main () {Dulnode *l;//head pointer int i,j,k; Elemtype e; Status s;initlist (&l);p rintf ("After initializing L: Listlength (l) =%d\n", Listlength (L)); for (j=1;j<=5;j++) I=listinsert (    L,1,J);p rintf ("After the table head in L is inserted in the following: L.data ="); Listtraverse (L);    printf ("Listlength (l) =%d \ n", Listlength (l));    I=listempty (L);    printf ("L" is empty: i =%d (1: Yes 0: NO) \ n ", i); I=clearlist (L);    printf ("Empty L: listlength (l) =%d\n", Listlength (l));    I=listempty (L);    printf ("L" is empty: i =%d (1: Yes 0: NO) \ n ", I); for (j=1;j<=10;j++) Listinsert (L,J,J);    printf ("After the footer of L is inserted in 1~10: L.data ="); Listtraverse (L); printf ("listlength (l) =%d \ n", Listlength (l));    Listinsert (l,1,0);    printf ("The table header in L is inserted after 0: L.data =");     Listtraverse (L); printf ("listlength (l) =%d \ n", Listlength (l));    Getelem (l,5,&e);        printf ("The value of the 5th element is:%d\n", e); for (j=3;j<=4;j++) {K=locateelem (l,j); if (k) printf ("The bit order of elements with value%d is%d\n"), j,k);    else printf ("no element with a value of%d \ n", j); }k=listlength (L); K is the table length for (j=k+1;j>=k;j--) {i=listdelete (l,j,&e);//Delete the first J data if (I==error) printf ("Delete        %d data failed \ n ", j);    else printf ("Delete element%d value is:%d\n", j,e);    } printf ("Output the elements of L sequentially:"); Listtraverse (L);    j=5; Listdelete (l,j,&e);    Delete the 5th Data printf ("Delete element%d value is:%d\n", j,e);    printf ("Output the elements of L sequentially:"); Listtraverse (L);    I=clearlist (L);    printf ("\ n empty L: listlength (l) =%d\n", Listlength (L)); for (j=1;j<=10;j++) Listinsert (L,J,J);    printf ("After the footer of L is inserted in 1~10: L.data ="); Listtraverse (L); printf ("listlength (l) =%d \ n", Listlength (L));p rintf ("\ n"); for (j=1;j<=10;j++) {S=priorelem (l,j,&e); if (s==error) printf ("element%d has no precursor \ n", j); elseprintf ("The precursor of element%d is%d\n", j,e);} printf ("\ nthe subsequent judgment \ n"); for (j=1;j<=10;j++) {S=nextelem (l,j,&e); if (s==error) printf ("element%d has no successor \ n", j); Elseprintf ( "The successor of element%d is%d\n", j,e);}}


Linear tables-04. Chain Storage structure (bidirectional loop linked list)

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.