[C language data structure] cyclic single linked list
CircleLinkList.h
# ifndef CIRCLE_LINK_LIST#define CIRCLE_LINK_LIST// linked list node typedef struct _ CircleLinkListNode {struct _ CircleLinkListNode * next;} CircleLinkListNode;// circular single linked list typedef void CircleLinkList;/* * create circular single linked list * @ return returns the pointer of the circular single linked list * / CircleLinkList* CircleLinkList_Create (); / * destroy the circular single linked list * @ param list the pointer of the circular single linked list * / void CircleLinkList_Destroy (CircleLinkList* list) / * * clear circular single linked list * @ param list circular single linked list pointer * / void CircleLinkList_Clear (CircleLinkList * list); / * * insert element * @ param list circular single linked list pointer * @ param node element pointer * @ param pos inserted index * / int CircleLinkList_Insert (CircleLinkList * list,CircleLinkListNode * node,int pos) / * * get the element at the index position in the circular single linked list * @ param list circular single linked list pointer * @ param pos circular single linked list index value * @ param return element pointer * / CircleLinkListNode* CircleLinkList_Get (CircleLinkList * list,int pos) / * * Delete the value at the index position in the circular single-linked list * @ param list the pointer to the circular single-linked list * @ param pos circular single-linked list index * @ param return non-zero indicates that the deletion was successful * / int CircleLinkList_Remove (CircleLinkList * list,int pos) / * * get the number of currently stored elements in the circular single linked list * @ param list pointer to the circular single linked list * @ return the number of stored elements in the circular single linked list * / int CircleLinkList_Length (CircleLinkList * list) / * * Delete a specific element in a circular single-linked list * @ param list pointer to a circular single-linked list * @ param pos circular single-linked list element pointer * @ param return non-zero indicates deletion succeeded * / int CircleLinkList_DeleteNode (CircleLinkList* list, CircleLinkListNode* node) / * * reset the cursor in the circular single-linked list, point to the pointer to the header * @ param list circular single-linked list * @ return returns the pointer to the element pointed by the cursor * / CircleLinkListNode* CircleLinkList_Reset (CircleLinkList* list); / * get the element pointer pointed to by the current cursor * @ param list circular single-linked list * @ return return the element pointer to the current cursor * / CircleLinkListNode* CircleLinkList_Current (CircleLinkList* list) / * * move the current cursor to the next element * @ pointer to the param list circular single linked list * @ return the pointer to the element pointed to by the cursor moved * / CircleLinkListNode* CircleLinkList_Next (CircleLinkList* list); # endif / / CIRCLECircleLinkList
CircleLinkList.c
# include "Circlelinklist.h" # include / / cyclic single linked list typedef struct _ CircleLinkList {CircleLinkListNode header;// chain header node CircleLinkListNode* cursor;// cursor linked list length} TCircleLinkList;/* * create circular single linked list * @ return return pointer to circular single linked list * / CircleLinkList* CircleLinkList_Create () {TCircleLinkList * list = (TCircleLinkList *) malloc (sizeof (TCircleLinkList)) If (list! = 0) {list- > header.next = 0; list- > length= 0;} return list;} / * * destroy circular single linked list * @ pointer to param list circular single linked list * / void CircleLinkList_Destroy (CircleLinkList * list) {free (list) } / * * clear the pointer to the circular single-linked list * @ param list circular single-linked list * / void CircleLinkList_Clear (CircleLinkList * list) {if (list! = 0) {TCircleLinkList * c_list = (TCircleLinkList *) clocked list; cased list-> header.next = 0; cased list-> length = 0; cantilel-> cursor = 0 }} / * * insert element into circular single linked list pos position * @ param list circular single linked list pointer * @ param node element pointer * @ param pos inserted index * / int CircleLinkList_Insert (CircleLinkList* list,CircleLinkListNode * node,int pos) {/ / Type conversion TCircleLinkList* l_list = (TCircleLinkList*) list / / determine whether the linked list pointer and node pointer cannot be null, and whether the current inserted position is legal int ret = ((list! = 0) & & (node! = 0) & & (pos > = 0) & & (pos length)); if (ret) {CircleLinkListNode* current = (CircleLinkListNode*) lumped list; int I / / move to the front for (I = 0; I) where you need to insert
< pos;i++) { current = current->Next;} node- > next = current- > next; / / the successor pointer of the inserted node points to the current- of the precursor node > next = node / / the successor pointer of the precursor node points to the inserted node / / if the insertion position is 0, you need to modify the tail node's successor pointer if (pos = = 0) {current = lumped list-> header.next; for (I = 0
< l_list->Length;i++) {current = current- > next;} current- > next = node;// changes the pointer of the tail node / / determines whether the insertion is an empty table if (lumped list-> length = = 0) {lumped list-> cursor = node; node- > next = node }} lumped list-> length++;} return ret;} / * * get the element * @ param list circular single linked list pointer * @ param pos circular single linked list index value * @ param return element pointer * / CircleLinkListNode* CircleLinkList_Get (CircleLinkList * list,int pos) {CircleLinkListNode* node = 0; TCircleLinkList * l_list = (TCircleLinkList *) list / / determine that the pointer to the linked list is not empty and the index obtained is legal if ((l_list! = 0) & & (pos > = 0)) {CircleLinkListNode* current = (CircleLinkListNode*) llistings; int i; for (I = 0; I)
< pos; i++) { current = current->Next;} node = current- > next;} return node;} / * * Delete the value at the index position in the circular single linked list * @ param list the pointer to the circular single linked list * @ param pos circular single linked list index * @ param return indicates that the deletion was successful * / int CircleLinkList_Remove (CircleLinkList * list,int pos) {TCircleLinkList * l_list = (TCircleLinkList *) list Int ret = (l_list! = 0) & & (pos > = 0) & & (pos
< l_list->Length); if (ret) {CircleLinkListNode* current = (CircleLinkListNode*) list; CircleLinkListNode* first = lumped list-> header.next; CircleLinkListNode* last = (CircleLinkListNode*) CircleLinkList_Get (l_list, lumped list-> length-1); CircleLinkListNode* del; int i; for (iTun0; inext;} del = current- > next; current- > next = del- > next Lumped list-> length--; if (first = = del) {lumped list-> header.next = del- > next; last- > next = del- > next;} if (lumped list-> cursor = = del) {lumped list-> cursor = del- > next } if (lumped list-> length = = 0) {lumped list-> header.next = 0; lumped list-> cursor = 0;}} return ret } / * * get the number of currently stored elements in the circular single linked list * @ param list pointer to the circular single linked list * @ return the number of stored elements in the circular single linked list * / int CircleLinkList_Length (CircleLinkList * list) {int ret =-1; if (list! = 0) {TCircleLinkList * l_list = (TCircleLinkList *) list; ret = lumped list-> length;} return ret } / * * Delete a specific element in a circular single linked list * @ param list circular single linked list element pointer * @ param pos circular single linked list element pointer * @ param return Index of deleted elements * / int CircleLinkList_DeleteNode (CircleLinkList* list, CircleLinkListNode* node) {int ret =-1; if ((list! = 0) & (node! = 0)) {TCircleLinkList * l_list = (TCircleLinkList *) list CircleLinkListNode* current = lumped list-> header.next; int i; for (I = 0; I
< l_list->Length; iTunes +) {if (node = = current) {CircleLinkList_Remove (lumped list I); ret = I; break;} current = current- > next;}} return ret } / * * reset the cursor in the circular single-linked list and point to the pointer to the header * @ param list circular single-linked list * @ return returns the pointer to the element pointed by the cursor * / CircleLinkListNode* CircleLinkList_Reset (CircleLinkList* list) {CircleLinkListNode* node = 0; if (list! = 0) {TCircleLinkList * l_list = (TCircleLinkList *) list; lumped list-> cursor = lumped list-> header.next Node = lcursor list-> cursor;} return node;} / * * get the element pointer to the current cursor * @ param list circular single linked list * @ return returns the element pointer to the current cursor * / CircleLinkListNode* CircleLinkList_Current (CircleLinkList* list) {CircleLinkListNode* node = 0; if (list! = 0) {TCircleLinkList * l_list = (TCircleLinkList *) list Node = lumped list-> cursor;} return node;} / * * move the pointer of the current cursor to the next element * @ param list circular single linked list * @ return the pointer of the cursor to the element after it is moved * / CircleLinkListNode* CircleLinkList_Next (CircleLinkList* list) {CircleLinkListNode* node = 0; if (list! = 0) {TCircleLinkList * l_list = (TCircleLinkList *) list Lumped list-> cursor = lumped list-> cursor- > next; node = lumped list-> cursor;} return node;}
Test code
# include # include "Circlelinklist.h" struct Value {CircleLinkListNode node; int val;}; int main (void) {struct Valueval [5]; int i; struct Value * p; CircleLinkList * list = CircleLinkList_Create (); for (I = 0
< 5;i++) { val[i].val = i; } for(i = 0;i < 5;i++) { CircleLinkList_Insert(list,(CircleLinkListNode *)&(val[i]),0); } // CircleLinkList_DeleteNode(list,&(val[0])); for(i = 0;i < 6;i++) { p = CircleLinkList_Get(list,i); printf("%d\n",p->Val); CircleLinkList_Next (list);} CircleLinkList_Reset (list); p = CircleLinkList_Get (list,0); printf ("% d\ n", p-> val); return 0;}