c语言不带头结点的循环链表joseph问题

 时间:2026-02-15 11:08:57

1、以下是不带头结点的循环链表算法

*/

#include <stdio.h>

#include <stdlib.h>

typedef int data_t;

typedef struct node

{

data_t data;

struct node *next;

}listnode, *linklist;

2、/*创建一个不带头节点的循环链表*/

listnode* CreatCycleList(int num)

{

int i = 2;

listnode *head = NULL, *q = NULL, *p = NULL;

head = (listnode*)malloc(sizeof(listnode));

head->data = 1;

head->next = NULL;

p = head;

while(i <= num)

{

q = (listnode*www.gzlij.com)malloc(sizeof(listnode));

q->data = i;

q->next = NULL;

p->next = q;

p = q;

i++;

}

p->next = head;

return head;

}

listnode* Joseph(listnode *head, int start, int killNum)

{

int i;

listnode *p, *q;      //p遍历链表,q指向待宰的人

p = head;

3、/* 找位置,p最后停在开始报数的前一个人处*/

if(start == 1)

{

while(p->next != head)

{

p = p->next;

}

}

else

{

for(i = 1; i < start-1; i++)

{

p = p->next;

}

}

4、/* 开杀*/

while(p != p->next)

{

for(i = 1; i < killNum; i++)

{

p = p->next;

}

q = p->next;

p->next = q->next;

printf("%d,",q->data);

free(q);

q = NULL;

}

return p;

}

void Display(listnode *head)

{

listnode *p = head;

while(p->next != head)

{

printf("%d,",p->data);

p = p->next;

}

printf("%d\n",p->data);

}

int main(int argc, char *argv[])

{

listnode *head = CreatCycleList(5);

Display(head);

listnode *p = Joseph(head, 8, 3);

printf("%d\n",p->data);

return 0;

}

  • 【校园指南】西南石油大学上课指导篇
  • Keep怎么关闭训练日历提醒?
  • Keep如何进行运动
  • 如何利用FLASH CS6组件制作视频播放器
  • flash中怎么让一个圆缓缓移动变形
  • 热门搜索
    芽庄旅游 旅游资讯 嘉兴旅游景点 榆林旅游 云南旅游报价 常州旅游商贸高等职业技术学校 重庆武隆旅游景点 宁德旅游 十渡旅游攻略 天府新区航空旅游职业学院