The difference between one-way circular linked list and ordinary linked list lies in : Of the last linked list of ordinary linked lists next point NULL, And the last node of the one-way circular linked list next Point to head node

  Header file :
#ifndef CIRCLELINKLIST #define CIRCLELINKLIST #include <stdio.h> #include
<stdlib.h> #include <string.h> // Linked list small node typedef struct CIRCLELINKNODE { struct
CIRCLELINKNODE* next; }CircleLinkNode; // Linked list structure typedef struct CIRCLELINKLIST {
CircleLinkNode head; int size; }CircleLinkList; // Define true and false macros #define
CIRCLELINKLIST_TRUE 1 #define CIRCLELINKLIST_FALSE 0 // Compare callback functions typedef
int(*COMPARENODE)(CircleLinkNode*, CircleLinkNode*); // Print callback function typedef
void(*PRINTNODE)(CircleLinkNode*); // For linked list structure operation API function // Linked list initialization function CircleLinkList*
initCircleLinkList(); // Insert function void insertByPos(CircleLinkList* clist, int pos,
CircleLinkNode* data); // Get the first element CircleLinkNode* getFront(CircleLinkList*
clist); // Delete by location void removeByPos(CircleLinkList* clist, int pos); // Delete by value void
removeByValue(CircleLinkList* clist, CircleLinkNode* data, COMPARENODE
compare); // Get the length of the linked list int getSize(CircleLinkList* clist); // Judge whether it is empty int
isEmpty(CircleLinkList* clist); // Find by value int findByValue(CircleLinkList* clist,
CircleLinkNode* data, COMPARENODE compare); // Print node void
printList(CircleLinkList* clist, PRINTNODE print); // Free memory void
freeSpace(CircleLinkList* clist); #endif
API realization :
#include "CircleLinkList.h" // For linked list structure operation API function // Linked list initialization function CircleLinkList*
initCircleLinkList() { CircleLinkList* clist =
(CircleLinkList*)malloc(sizeof(CircleLinkList)); // Linked list data initialization clist->head.next =
&(clist->head); clist->size = 0; return clist; } // Insert function void
insertByPos(CircleLinkList* clist, int pos, CircleLinkNode* data) { if (clist
== NULL) { return; } if (data == NULL) { return; } if (pos < 0 || pos >
clist->size) { pos = clist->size; } // Find the precursor node CircleLinkNode* pCurrent =
&(clist->head); for (int i = 0; i < pos; i++) { pCurrent = pCurrent->next; }
// Node insertion data->next = pCurrent->next; pCurrent->next = data; clist->size++; }
// Get the first element CircleLinkNode* getFront(CircleLinkList* clist) { return
clist->head.next; } // Delete by location void removeByPos(CircleLinkList* clist, int pos) {
if (clist == NULL) { return; } if (pos < 0 || pos > clist->size) { return; }
CircleLinkNode* pCurrent = clist->head.next; for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next; } // Cache the next node of the current node CircleLinkNode* pNext =
pCurrent->next; pCurrent->next = pNext->next; clist->size--; } // Delete by value void
removeByValue(CircleLinkList* clist, CircleLinkNode* data, COMPARENODE compare)
{ if (clist == NULL || data == NULL) { return; } CircleLinkNode* pPrev =
&(clist->head); CircleLinkNode* pCurrent = pPrev->next; for (int i = 0; i <
clist->size; i++) { if (compare(pCurrent, data) == CIRCLELINKLIST_TRUE) {
pPrev->next = pCurrent->next; clist->size--; break; } pPrev = pCurrent;
pCurrent = pCurrent->next; } } // Get the length of the linked list int getSize(CircleLinkList* clist) {
return clist->size; } // Judge whether the linked list is empty int isEmpty(CircleLinkList* clist) { if
(clist->size == 0) { return CIRCLELINKLIST_TRUE; } return CIRCLELINKLIST_FALSE;
// return 0 express , Not empty } // Find by value int findByValue(CircleLinkList* clist, CircleLinkNode*
data, COMPARENODE compare) { if (clist == NULL || data == NULL) { return -1; }
CircleLinkNode* pCurrent = clist->head.next; int flag = -1; for (int i = 0; i <
clist->size; i++) { if (compare(pCurrent, data) == CIRCLELINKLIST_TRUE) { flag
= i; break; } pCurrent = pCurrent->next; } return flag; } // Print node void
printList(CircleLinkList* clist, PRINTNODE print) { if (clist == NULL) {
return; } CircleLinkNode* pCurrent = clist->head.next; for (int i = 0; i <
clist->size; i++) { print(pCurrent); pCurrent = pCurrent->next; } } // Free memory void
freeSpace(CircleLinkList* clist) { if (clist == NULL) { return; } free(clist); }
Test code :
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h>
#include <string.h> #include "CircleLinkList.h" // Test structure typedef struct PERSON {
CircleLinkNode node; // Connect various data char name[64]; int age; }Person; void
myPrint(CircleLinkNode * data) { Person* person = (Person*)data;
printf("name:%s, age:%d\n", person->name, person->age); } //1 Represents the same ,0 Represent different int
myCompare(CircleLinkNode* data1, CircleLinkNode* data2) { Person* p1 =
(Person*)data1; Person* p2 = (Person*)data2; if (strcmp(p1->name, p2->name) ==
0 && p1->age == p2->age) { return CIRCLELINKLIST_TRUE; } return
CIRCLELINKLIST_FALSE; } void test01() { CircleLinkList* clist =
initCircleLinkList(); // Create test data Person p1, p2, p3, p4, p5, p6; strcpy(p1.name,
"aaa"); strcpy(p2.name, "bbb"); strcpy(p3.name, "ccc"); strcpy(p4.name, "ddd");
strcpy(p5.name, "eee"); strcpy(p6.name, "fff"); p1.age = 30; p2.age = 40;
p3.age = 50; p4.age = 60; p5.age = 70; p6.age = 80; // Insert data into linked list structure
insertByPos(clist, 0, (CircleLinkNode*)(&p1)); insertByPos(clist, 0,
(CircleLinkNode*)(&p2)); insertByPos(clist, 0, (CircleLinkNode*)(&p3));
insertByPos(clist, 0, (CircleLinkNode*)(&p4)); insertByPos(clist, 0,
(CircleLinkNode*)(&p5)); insertByPos(clist, 0, (CircleLinkNode*)(&p6)); // print data
printList(clist, myPrint); printf("===============================\n"); // Delete data
removeByPos(clist, 2); printList(clist, myPrint);
printf("===============================\n"); // Find data Person findP;
strcpy(findP.name, "ccc"); findP.age = 50; int flag = findByValue(clist,
&findP, myCompare); if (flag == CIRCLELINKLIST_TRUE) { printf(" data %s eureka \n",
findP.name); } else { printf(" data %s Not found \n", findP.name); } } int main(void) {
test01(); system("pause"); return 0; }
josephus problem :

The source of Joseph's problem can be named “ Suicide game ”. In line with the purpose of harmony, friendship and pursuit of essence , We describe the problem as follows :

* existing M Sit down around a table , Number from 1 reach M, From No 1 Of people began to count off .
* Count off from 1 start , Check in N People leave the table , Start with the next member of the audience who leaves , Continue from 1 Start counting .
* Reproduce this process ( Order of departure of members ), Or ask for the number of the last member present . #define _CRT_SECURE_NO_WARNINGS #include
<stdio.h> #include <stdlib.h> #include <string.h> #include "CircleLinkList.h"
// Total data in the ring ,N Step out of the line #define M 8 #define N 3 // Stored data typedef struct MYNUM {
CircleLinkNode node; int val; // Stored data number }MyNum; // Print callback implementation void
myPrint1(CircleLinkNode* data) { MyNum* num = (MyNum*)data; printf("%d ",
num->val); } // Compare callback printing int myCompare1(CircleLinkNode* data1, CircleLinkNode*
data2) { MyNum* m1 = (MyNum*)data1; MyNum* m2 = (MyNum*)data2; if (m1->val ==
m2->val) { return CIRCLELINKLIST_TRUE; } return CIRCLELINKLIST_FALSE; } void
josephus() { // Create a circular linked list CircleLinkList* clist = initCircleLinkList(); // Insert data into linked list
MyNum myNum[8]; // Data array for (int i = 0; i < 8; i++) { myNum[i].val = i + 1;
insertByPos(clist, i, (CircleLinkNode*)&myNum[i]); } // Print in loop data printList(clist,
myPrint1); printf("\n"); int index = 1; // Indicates the current location CircleLinkNode* pCurrent =
clist->head.next; while (getSize(clist) > 1) { if (index == N) { MyNum* tempNum
= (MyNum*)pCurrent; printf("%d ", tempNum->val); // Cache the next node of the node to be deleted
CircleLinkNode* pNext = pCurrent->next; // Delete by value removeByValue(clist, pCurrent,
myCompare1); // Determine whether it is a header node , Yes, you need to jump over pCurrent = pNext; if (pCurrent ==
&(clist->head)) { pCurrent = pCurrent->next; } // Reset index value index = 1; } pCurrent
= pCurrent->next; if (pCurrent == &(clist->head)) { pCurrent = pCurrent->next;
} index++; } printf("\n"); if (getSize(clist) == 1) { MyNum* font =
(MyNum*)getFront(clist); printf(" The final screening is %d\n", font->val); } else {
printf(" Filtering error !!!!\n"); } // Free linked list memory freeSpace(clist); } int main(void) {
josephus(); return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?