Design of circular chain list and implementation of API

Source: Internet
Author: User

Basic concepts
Definition of a circular list: The next pointer to the last data element in a single linked list points to the first element

Circular linked list all operations with single-linked list

Create a linked list
Destroy linked list
Get the chain table length
Empty list
Get the first POS element operation
Insert element to position POS
Delete the element at position Pos

What's NEW: cursor definition

In a circular list, you can define a "current" pointer, which is often called a cursor that iterates through all the elements in a linked list.

Circular linked list new operations

Resets a cursor to the first data element in a linked list
circlelistnode* circlelist_reset (circlelist* list);

Gets the data element that the current cursor points to
circlelistnode* circlelist_current (circlelist* list);

Move the cursor to the next data element in the linked list
circlelistnode* circlelist_next (circlelist* list);

Directly specify to delete a data element in a linked list
circlelistnode* Circlelist_deletenode (circlelist* list, circlelistnode* node);
Remove an element based on the value of an element pk deletes an element based on its position

Finally, the application of a circular list is added: solving the Joseph problem

Joseph problem-Circular list typical applications
n the individual is surrounded by a circle, first of all, the 1th person starts from 1, a person by a person clockwise count, report to the first m person, make it out. And then from the next person starting from 1 clockwise count, report to the first m person, then make it out, ..., so go, ask for the order of the dequeue.

Code:

circlelist.h//cyclic link List API declaration #ifndef _circlelist_h_#define _circlelist_h_typedef void circlelist;typedef struct _TAG_ Circlelistnode{struct _tag_circlelistnode *next;} circlelistnode;//Create a linked list circlelist* circlelist_create ();//Destroy the list void Circlelist_destroy (circlelist* list);//Empty list void Circlelist_clear (circlelist* list);//Gets the length of the list int circlelist_length (circlelist* list);//Insert Node Nodeint circlelist at POS location _insert (circlelist* list,circlelistnode* node, int pos);//Get node circlelistnode* circlelist_get for POS locations (circlelist* list, int pos);//delete node of POS location circlelistnode* circlelist_delete (circlelist* list, int pos);//Data deletion based on node value circlelistnode* Circlelist_deletenode (circlelist* list, circlelistnode* node);//Reset Cursor circlelistnode* circlelist_reset (CircleList* list);//Gets the node circlelistnode* circlelist_current (circlelist* list) of the current cursor;//Returns the node of the original cursor to the upper layer, Then let the cursor move to the next node circlelistnode* circlelist_next (circlelist* list); #endif

circlelist.cpp//Loop List API Implementation # include <iostream> #include <cstdio> #include "circlelist.h" typedef struct _ Tag_circlelist{circlelistnode header; Circlelistnode *silder;int length;} tcirclelist;//Create a linked list circlelist* circlelist_create () {tcirclelist *ret = (tcirclelist *) malloc (sizeof (tcirclelist)); RET = = null) {return null;} Initialize Ret->header.next = Null;ret->silder = Null;ret->length = 0;return ret;} Destroys the linked list void Circlelist_destroy (circlelist* list) {if (list = = NULL) {return;} Free (list); return;} Empties the list void Circlelist_clear (circlelist* list) {if (list = = NULL) {return;} Tcirclelist *tlist = (tcirclelist *) List;tlist->header.next = Null;tlist->silder = NULL;tList->length = 0; return;} Gets the length of the linked list int circlelist_length (circlelist* list) {if (list = = NULL) {return-1;} Tcirclelist *tlist = (tcirclelist *) List;return tlist->length;} Insert Node Nodeint circlelist_insert (circlelist* list, circlelistnode* node, int pos) at POS location {if (list = = NULL | | node = = NULL | | POS < 0) {return-1;} Tcirclelist *tlist = (tcirclelist *) list; Circlelistnode *cur = (Circlelistnode *) tlist;for (int i = 0; i < POS; ++i) {cur = cur->next;} Node->next = Cur->next;cur->next = node;//If it is inserted for the first time if (tlist->length = = 0) {tlist->silder = node;} ++tlist->length; Remember the length plus 1//if it is the head interpolation if (cur = = (Circlelistnode *) tList) {//Get last element Circlelistnode *last = Circlelist_get (TList, Tlist->l ENGTH-1); last->next = Cur->next;} return 0;} Gets the POS location node circlelistnode* circlelist_get (circlelist* list, int pos) {//because it is a circular linked list, so there is no need to exclude the case of pos>length if (list = = NULL | | POS < 0) {return NULL;} Tcirclelist *tlist = (tcirclelist *) list; Circlelistnode *cur = (Circlelistnode *) tlist;for (int i = 0; i < POS; ++i) {cur = cur->next;} return cur->next;} Delete node of POS location circlelistnode* circlelist_delete (circlelist* list, int pos) {tcirclelist *tlist = (tcirclelist *) list; Circlelistnode *ret = null;if (tList! = NULL && pos >= 0 && tlist->length > 0) {circlelistnode *cur = (Circlelistnode *) tlist;for (int i = 0; i < POS; ++i) {cur = cur->next;} If you delete the head node, you need to request the tail node Circlelistnode *last = null;if (cur = = (Circlelistnode *) tList) {last = Circlelist_get (TList, tlist-> LENGTH-1);} ret = Cur->next;cur->next = ret->next;--tlist->length;//If delete head node if (last! = NULL) {Tlist->header.next = ret ->next;last->next = Ret->next;} If the element being deleted is the element that the cursor refers to if (Tlist->silder = = ret) {tlist->silder = Ret->next;} If you delete an element, the list length is 0if (tlist->length = = 0) {Tlist->header.next = Null;tlist->silder = NULL;}} return ret;} Data deletion based on the value of the node circlelistnode* circlelist_deletenode (circlelist* list, circlelistnode* node) {tcirclelist *tlist = ( Tcirclelist *) List; Circlelistnode *ret = null;if (list! = NULL && node! = null) {Circlelistnode *cur = (Circlelistnode *) Tlist;int I = 0;for (i = 0; i < tlist->length; ++i) {if (Cur->next = = node) {ret = Cur->next;break;} cur = cur->next;} If you find if (ret! = NULL) {circlelist_delete (tList, i);}} return ret;} Reset cursor circlelistnode* circlelist_reset (circlelist* list) {tcirclelist *tlist = (tcirclelist *) list; circlelistnode* ret = null;if (list! = NULL) {Tlist->silder = Tlist->header.next;ret = Tlist->silder;} return NULL;} Gets the node that the current cursor refers to circlelistnode* circlelist_current (circlelist* list) {tcirclelist *tlist = (tcirclelist *) list; circlelistnode* ret = null;if (list! = NULL) {ret = Tlist->silder;} return ret;} Returns the node of the original cursor to the upper layer and then moves the cursor to the next node circlelistnode* circlelist_next (circlelist* list) {tcirclelist *tlist = (tcirclelist *). List circlelistnode* ret = null;if (list! = NULL && Tlist->silder! = null) {ret = Tlist->silder;tlist->silder = Ret->next;} return ret;}

joseph.h//using the cyclic link List API to solve Joseph problem # include <cstdio> #include "circlelist.h" const int MAXP = 8;struct person{ Circlelistnode circlenode;int ID;}; void Joseph () {person s[maxp];for (int i = 0; i < Maxp; ++i) {s[i].id = i + 1;} Circlelist *list = Null;list = Circlelist_create ();//insert element for (int i = 0; i < Maxp; ++i) {//tail interpolation int ret = circlelist_in SERT (list, (Circlelistnode *) &s[i], circlelist_length (list)), if (Ret < 0) {printf ("function Circlelist_insert ERR:%d\n ", ret);}} Traverse the list for (int i = 0; i < circlelist_length (list); ++i) {Person *tmp = (person *) Circlelist_get (list, i); if (tmp = = NU LL) {printf ("function Circlelist_get err.\n");} printf ("Age:%d\n", tmp->id);} Solve Joseph Problem while (Circlelist_length (list) > 0) {person* PV = null;for (int i = 1; i < 3; i++) {circlelist_next (list);} PV = (person*) circlelist_current (list);p rintf ("%d", pv->id); Circlelist_deletenode (list, (Circlelistnode *) PV); The node element is deleted}printf ("\ n") based on the value of the node. Circlelist_destroy (list);}

main.cpp//Cyclic link list Test program # include <iostream> #include <cstdio> #include "circlelist.h" #include "joseph.h" const int MAXN = 5;struct student{circlelistnode Circlenode;char name[32];int age;}; void Play01 () {Student s[maxn];for (int i = 0; i < MAXN; ++i) {s[i].age = i + 1;} Circlelist *list = Null;list = Circlelist_create (); Create a linked list//insert element for (int i = 0; i < MAXN; ++i) {//tail interpolation int ret = Circlelist_insert (list, (Circlelistnode *) &s[i], CIRC Lelist_length (list)), if (Ret < 0) {printf ("function Circlelist_insert err:%d\n", ret);}} Traverse the chain list//here to traverse the print side, can prove that this is a circular link list for (int i = 0; i < 2 * Circlelist_length (list); ++i) {Student *tmp = (Student *) circlel Ist_get (list, i), if (tmp = = NULL) {printf ("function Circlelist_get err.\n");} printf ("Age:%d\n", tmp->age);} Delete node, through node location while (Circlelist_length (list)) {Student *tmp = (Student *) circlelist_delete (list, circlelist_length ( List)-1); if (tmp = = NULL) {printf ("function Circlelist_delete err.\n");} printf ("Age:%d\n", tmp->age);}//destroy linked list Circlelist_destroy (list);} int main () {play01 ();//In order to test the life cycle of the data, write another function call to run Joseph (); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design of circular chain list and implementation of API

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.