1. Overview:
For a circular list, the first node and the distal point are connected together. This method can be realized in one-way and bidirectional linked lists. To convert a circular list, you can choose to start at any node and then follow either direction of the list until you return to the beginning node. To look at another method, a circular list can be considered "Woold". This list is good for saving the data store cache, assuming you have an object in a list and want all other objects to iterate in a special arrangement.
A pointer to an entire list can be called an access pointer.
A circular list constructed by one-way linked list
The first node in the Loop list is the last node, and vice versa . The borderless chain lists make it easier to design algorithms on such lists than regular lists. There is little difference (see example code below) as to whether the newly added node should be flexible to the actual requirements after the first node or the last node. Of course, if you only insert data at the end (or just before), it's also easy to process.
There is also a simulation of the circular chain, which is accessed to the last node, the manual jump to the first node. The same is the case before accessing the first node. This can also achieve the function of the circular chain list, in the direct use of the cycle linked list is more troublesome or may have problems when you can use.
2. Program implementation:
/* c2-2.h linear table of the single linked table storage structure
/struct Lnode
{
elemtype data;
struct Lnode *next;
typedef struct LNODE *linklist; /* Another way to define linklist * *
/* BO2-4.C 12 basic operations to establish a single cycle list of tail pointers (the storage structure is defined by c2-2.h)/Status initlist_cl (linklist *l) {/* operation result: Construct an empty linear table L/*l= (linklist ) malloc (sizeof (struct lnode));
/* Generate header node, and make L point to this head node/if (!*L)/* Storage allocation failure/exit (OVERFLOW); (*l)->next=*l;
/* Pointer field point to head node/return OK; Status destroylist_cl (linklist *l) {/* action result: destroy linear table L/linklist q,p= (*l)->next;/* p Point Header/while (P!=*L)/* not yet.
Table Tail * * q=p->next;
Free (p);
p=q;
Free (*l);
*l=null;
return OK; Status clearlist_cl (linklist *l)/* Change L/{*/* initial condition: Linear table L already exists.
Operation Result: Reset L to Empty table * * * linklist p,q; *l= (*l)->next; /* L Point head node/p= (*L)->next;
/* p points to the first node/while (P!=*L)/* Not to the footer/{q=p->next;
Free (p);
p=q; } (*l)->next=*l;
/* head node pointer field pointing to itself/return OK; Status listempty_cl (linklist l) {//* initial condition: Linear table L already exists.
Operation Result: If L is an empty table, returns True, otherwise returns false/if (L->NEXT==L)/* NULL/return true;
else return FALSE; ' int listlength_cl (linklist L) {//* initial condition: L already exists.
Operation Result: Returns the number of data elements in L/int i=0; LiNklist p=l->next;
/* P point head node/while (P!=L)/* Not to the end of the table/{i++;
p=p->next;
return i; The Status getelem_cl (linklist l,int i,elemtype *e) {/* When the first element exists, its value is assigned to E and returned OK, otherwise the error/int j=1 is returned;/* initialized, J is counter */Li Nklist p=l->next->next; /* p points to the first node */if (i<=0| |
I>LISTLENGTH_CL (L))/* I element does not exist/return ERROR;
while (j<i) {* * The pointer looks backward until p points to the first element/* p=p->next;
j + +; } *e=p->data;
* * Take the first element of I/return OK;
int Locateelem_cl (linklist l,elemtype e,status (*compare) (Elemtype,elemtype)) {/* Initial condition: Linear table L already exists, compare () is the data element decision function. /* Operation Result: Returns the bit order of the data element in L 1th that satisfies the relationship compare ().
* * * If such data element does not exist, then the return value is 0 */int i=0; Linklist p=l->next->next;
/* p points to the first node */while (p!=l->next) {i++;
if (compare (p->data,e))/* satisfies the relationship/return i;
p=p->next;
return 0; Re_e returns to its predecessor, * * Otherwise the operation fails, Pre_e noDefinition */linklist q,p=l->next->next;
/* p points to the first node * * q=p->next;
while (q!=l->next)/* p not to the footer/{if (q->data==cur_e) {*pre_e=p->data;
return TRUE;
} p=q;
q=q->next;
return FALSE; Next_e returns its successor, * * or operation failure, next_e no definition * * * linklist p=l->next->next;
/* p points to the first node/while (P!=L)/* p not to the footer/{if (p->data==cur_e) {*next_e=p->next->data;
return TRUE;
} p=p->next;
return FALSE; Status listinsert_cl (linklist *l,int i,elemtype e)/* Change L/{* * insert element E/linklist p= (*l)->next,s;/* Before position I of L
P-Pointing head node/int j=0; if (i<=0| |
I>LISTLENGTH_CL (*l) +1)/* Cannot insert/return ERROR before the first element;
while (j<i-1)/* Find the first i-1 node/{p=p->next;
j + +; } s= (linklist) malloc (sizeof (struct lnode)); /* Generate new node * * s->data=e;
* * Insert L/s->next=p->next;p->next=s;
if (p==*l)/* Change the tail node * * *L=S;
return OK; Status listdelete_cl (linklist *l,int i,elemtype *e)/* Change L/{* * Delete l elements, and return their value by e * * linklist p= (*l)->next,q;
/* P pointing head node/int j=0; if (i<=0| |
I>LISTLENGTH_CL (*L))/* I element does not exist/return ERROR;
while (j<i-1)/* Find the first i-1 node/{p=p->next;
j + +; } q=p->next;
/* Q point to delete node * * p->next=q->next;
*e=q->data;
if (*l==q)/* Deleted is the footer element * * *L=P; Free (q);
/* Release pending deletion of the node/return OK; Status listtraverse_cl (linklist l,void (*VI) (elemtype)) {*/* initial condition: L already exists. Operation Result: the function VI () is called for each data element of L in turn.
Once VI () fails, the operation fails/linklist p=l->next->next;
while (P!=l->next) {VI (P->DATA);
p=p->next;
printf ("\ n");
return OK;
}
/* main2-4.c single cycle chain list, test bo2-4.c main program * * * #include "c1.h" typedef int elemtype;
#include "c2-2.h" #include "bo2-4.c" Status compare (elemtype c1,elemtype C2) {if (C1==C2) return TRUE;
else return FALSE;
} void Visit (Elemtype c) {printf ("%d", c);
} void Main () {linklist L;
Elemtype e;
Int J;
Status i; I=INITLIST_CL (&L);
/* Initialize single loop chain list L/printf ("Initialize single cycle list L i=%d (1: initialization succeeded) \ n", i);
I=LISTEMPTY_CL (L);
printf ("L Whether empty i=%d (1: null 0: NO) \ n", i); LISTINSERT_CL (&l,1,3);
/* in L, insert 3,5/LISTINSERT_CL (&l,2,5) in turn;
I=GETELEM_CL (l,1,&e);
J=LISTLENGTH_CL (L); The number of data elements in printf ("L") is =%d, and the 1th element has a value of%d.
\ n ", j,e);
printf (The Data elements in "L are:");
LISTTRAVERSE_CL (L,visit); PRIORELEM_CL (l,5,&e); * * For the precursor of element 5/printf ("5 of the elements before the value is%d.")
\ n ", e); NEXTELEM_CL (l,3,&e); /* Ask for the successor of element 3/printf (the value of the element after 3 is%d.
\ n ", e);
printf ("L is empty%d (1: null 0: NO) \ n", LISTEMPTY_CL (l));
J=LOCATEELEM_CL (L,5,compare); if (j) printf (the first%d element of "L" is 5.)
\ n ", j);
else printf ("no element with a value of 5 \ n"); I=LISTDELETE_CL (&L,2,&E);
printf ("Delete the 2nd element of L: \ n");
if (i) {printf ("The deleted element value is%d, now the data elements in L are:", e);
LISTTRAVERSE_CL (L,visit); else printf (delete unsuccessful!)
\ n ");
printf ("Empty l:%d (1: successful) \ n", Clearlist_cl (&l));
printf ("Empty L, L is null:%d (1: null 0: NO) \ n", LISTEMPTY_CL (l));
printf ("Destroy l:%d (1: Success) \ n", Destroylist_cl (&l)); }