Cyclic unidirectional link list (Joseph Ring)

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>typedefstructlist{intdata; structList *Next;} List;//Create circular unidirectional linked list N for lengthList *list_create (intN) {List*head, *p; inti; Head= (List *)malloc(sizeof(List)); P=Head; P->data =1;//Create a first node     for(i =2; I <= N; i++)//Create the remaining nodes.{p->next = (List *)malloc(sizeof(List)); P= p->Next; P->data =i; } P->next =Head; returnhead;}//Print cycle linked listvoidList_print (List *head) {List*index; printf ("%d\t", Head->data);//Output First nodeindex = head->Next;  while(Index! = head)//determine if it is the last node.{printf ("%d\t",index->data); Index= index->Next; }}//Statistics cycle chain list lengthintList_len (List *head) {List*index; intCount =0; Index= head->Next; Count=1;  while(Index! =head); {Count++; Index= index->Next; } printf ("\n%d\n", Count); returncount;}//Insert Data Index Insert position, data insertList *list_addnote (list *head,intIndexintdate) {List*p,*pnew,*Q; inti =0; P=Head;  while(I < index-1)//Find Nodes{p= p->Next; I++; } pnew= (List *)malloc(sizeof(List));//request space for a new nodePnew->data =date; Q= p->next;//Save the post-insertion nodeP->next =pnew; Pnew->next =Q; returnhead;}//Delete a node with a value of dateList *list_delnote (list *head,intdate) {List*p,*per; Per=Head; P= head->Next;  while(P->data! = date)//Find Nodes{per= P;//precursor junction.p = p->Next; } per->next = p->Next; if(p = = head)//If you delete a head node, the head node should point to the nextHead = head->Next;  Free(P); returnhead;}//Joseph Ring known n individuals (denoted by number 1,2,3...N) sit around a table. //from the number of people numbered K began to count, the number to m of the person out of the person, and his next man from 1 began counting,//The man who counts to M is out of the way, and repeats until all the people around the table are out.voidList_process (List *head,intKintm) {List*p, *Q; inti =1; P=Head;  while(I < k)//find the location of the number K{p= p->Next; I++; }     while(P->next! = p)//determine if there is only one last node left.    {         for(i =1; I < M-1; i++)//Start count off{p= p->Next; } q= p->next;//data that needs to be deletedP->next = p->next->next;//point the deleted space to the transitionp = p->Next; printf ("%d\t",q->data);  Free(q); } printf ("%d\t", P->data);//last one     Free(P);}intMainvoid) {List*Head; Head= List_create (Ten); Head= List_addnote (Head,5, -); Head= List_delnote (Head,1);    List_print (head); printf ("\ n"); List_process (Head,2,3); System ("Pause"); return 0;}

Cyclic unidirectional link list (Joseph Ring)

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.