本文主要是介绍约瑟夫问题(没有头节点的循环链表2——删除法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
#include <stdlib.h>struct student{int id;struct student * next;
};struct student * tailCreateCircleList(struct student * L,int num){//没有头节点的循环链表struct student * p, * last;L = (struct student *)malloc(sizeof(struct student));L->id=0;last = L;printf("请依次输入学生编号:");for(int i=0;i<num;i++){p = (struct student *)malloc(sizeof(struct student));scanf("%d",&p->id);last->next=p;last=p;p->next=NULL;}p->next=L->next;return L;
}struct student * searchStudent(struct student * L,int m){struct student * p;p = L->next;int i;for(i=1;i<m;i++){p=p->next;}return p;
}struct student * deleteStudent(struct student * L,struct student * target){struct student * p;p=L->next;while(p->next!=target){p=p->next;}printf("%d ",p->next->id);p->next=target->next;free(target);return p;
}int menu(){struct student * L;printf("请依次输入学生人数:");int n;scanf("%d",&n);L=tailCreateCircleList(L,n);printf("一共有学生%d人\n",n);printf("依次被淘汰的学生为:");for(int i=n;i>1;i--){L=deleteStudent(L,searchStudent(L,3));}printf("\n获胜者:%d\n",L->id);free(L);return 0;
}int main(){menu();return 0;
}
这篇关于约瑟夫问题(没有头节点的循环链表2——删除法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!