约瑟夫环问题解决

这篇具有很好参考价值的文章主要介绍了约瑟夫环问题解决。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

链表

struct List
{
	int data;
	struct List* next;
}

创建链表

单链表
实现
struct List* listCreate()
{
	int data;
    struct List* head = NULL;
    struct List* pre = NULL;
    struct List* current = NULL;
    
    while(scanf("%d",&data) && data != -1)
    {
        current = (struct List*)malloc(sizeof(struct List));
        if(head == NULL)
            head = current;
        else
            pre->next = current;
        current->next = NULL;
        current->data = data;
        pre = current;
    }
    return head;
}
错例
struct List* listCreate()
{
	int data;;
	struct List* current = NULL;
	struct List* head = current;
	while (scanf("%d", &data) && data != -1)
	{
		current = (struct List*)malloc(sizeof(struct List));
		if (head == NULL)
			head = current;
		current->data = data;
		current = current->next;
	}
	return head;
}

在使用malloc函数开辟的空间中,不要进行指针的移动,因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配文章来源地址https://www.toymoban.com/news/detail-787359.html

循环链表

单独创建
struct List* circle_listCreate()
{
	int data;
    struct List* head = NULL;
    struct List* pre = NULL;
    struct List* current = NULL;
    
    while(scanf("%d",&data) && data != -1)
    {
        current = (struct List*)malloc(sizeof(struct List));
        if(head == NULL)
            head = current;
        else
            pre->next = current;
        current->next = head;
        current->data = data;
        pre = current;
    }
    return head;
}
逐节点创建
void Append(struct List** L,int data)
{
    struct List* head = *L;
    struct List* newNode = NULL;
    if((*L) == NULL)
    {
        (*L) = (struct List*)malloc(sizeof(struct List));
        (*L)->data = data;
        head = (*L);
        (*L)->next = head;
    }
    else
    {
        while ((*L)->next != head){
            (*L) = (*L)->next;
        }
        newNode = (struct List*)malloc(sizeof(struct List));
        newNode->data = data;
        (*L)->next = newNode;
        newNode->next = head;
        *L = head;
    }
}
约瑟夫环问题
void Append(struct List** L,int data)
{
    struct List* head = *L;
    struct List* newNode = NULL;
    if((*L) == NULL)
    {
        (*L) = (struct List*)malloc(sizeof(struct List));
        (*L)->data = data;
        head = (*L);
        (*L)->next = head;
    }
    else
    {
        while ((*L)->next != head){
            (*L) = (*L)->next;
        }
        newNode = (struct List*)malloc(sizeof(struct List));
        newNode->data = data;
        (*L)->next = newNode;
        newNode->next = head;
        *L = head;
    }
}
void Display(struct List* L,int num)
{
    struct List* head = L;
    struct List* pre = NULL;
    struct List* kill = NULL;
    int nodeNum = 0;
    while (L->next != head)
    {
        nodeNum++;
        L = L->next;
    }
    pre = L;
    L = L->next;
    nodeNum++;
    while (nodeNUm)
    {
        if (nodeNum == 1)
        {
            printf("%d",L->data);
            free(L);
            return;
        }
        for (int i=1; i < m; i++)
        {
            pre = L;
            L = L->next;
        }
        printf("%d ", L->data);
        kill = L;
        L = L->next;
        free(kill);
        nodeNum--;
    }
}

删除节点

实现方式一:

struct list* listDelete(struct list* L,int data)
{
    struct list* pre = L;
    struct list* head = L;
    struct list* kill;
    
    while(head != NULL && head->data == m)
    {
        kill = head;
        head = head->next;
        free(kill);
    }
    if(head == NULL)
        return head;
    
    pre = head;
    kill = head->next;
    while(kill!=NULL)
    {
        if(kill->data == m)
        {
            pre->next = kill->next;
            free(kill);
            kill = pre->next;
        }
        else
        {
            pre = kill;
            kill = kill->next;
        }
    }
    return head;
}

实现方式二:

struct list* listDelete(struct list** L,int data)
{
    struct list* head = (*L), * pre = (*L);
    struct list* newL = *L;
    struct list* kill = NULL;
    while (*L !- NULL)
    {
        if((*L)->data == data)
        {
            if((*L) == newL)
                newL == newL->next;
            else
                pre->next = (*L)->next;
            kill = (*L);
            (*L) = (*L)->next;
            free(kill);
        }
        else
        {
            pre = (*L);
            (*L) = (*L)->next;
        }
    }
    *L = newL;
    return head;
}

删除节点并建立新链表

struct list* list_Delete_Create(struct list** L) //数据为奇数存为新链表
{
    struct list* newhead = NULL, * newcurrent = NULL, * newpre = NULL;
    struct list* newL = *L;
    struct list* kill = NULL;
    struct list* pre = *L;
    while (*L)
    {
        if((*L)->data%2 == 1)
        {
            newcurrent = (struct list*)malloc(sizeof(struct list));
            if(newhead == NULL)
                newhead = newcurrent;
            else
                newpre->next = newcurrent;
            newcurrent->data = (*L)->data;
            newcurrent->next = NULL;
            newpre = newcurrent;
            
            if((*L) == newL)
                newL = newL->next;
            else
                pre-next = (*L)->next;
            kill = (*L);
            (*L)=(*L)->next;
            free(kill);
        }
        else
        {
            pre = (*L);
            (*L) = (*L)->next;
        }
    }
    *L = newL;
    return newhead;
}

逆置链表

实现

到了这里,关于约瑟夫环问题解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 重温数据结构与算法之约瑟夫问题

    约瑟夫问题 ,是一个计算机科学和数学中的问题,在计算机编程的算法中,类似问题又称为 约瑟夫环 ,又称“丢手绢问题”。 据说著名犹太历史学家 Josephus 有过以下的故事: 在罗马人占领乔塔帕特后,39个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也

    2024年02月08日
    浏览(31)
  • 数据结构—约瑟夫环问题(C语言版)

    目录 首先什么是约瑟夫环 约瑟夫环实现方式 一、创建结构体变量 二、初始化链表 三、构建循环链表 四、删除链表  五、完整代码及注释讲解 约瑟夫环 是 循环链表 中的一个经典问题;题目描述:n 个人围成一圈,从第一个人开始报数,数到 m 的人出列,再由下一个人重新

    2024年02月11日
    浏览(32)
  • 数据结构学习-循环链表:处理约瑟夫环问题

    目录 问题描述 一、基本概念  1.普通链表 2.单向循环链表  二、问题处理 1.创建链表 2.查找 3.删除  4.其他  三.实验环节 四.总结 约瑟夫环问题的一种描述是:编号为1,2,...,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。一开始任选一个正整数作为报数

    2024年02月07日
    浏览(29)
  • 【数据结构】使用循环链表结构实现约瑟夫环问题

    目录 1.循环链表的定义 2.约瑟夫环问题 3.创建循环链表 4.删除节点操作 5.打印所有节点 6.实现约瑟夫环问题的完整程序代码 🌈嗨!我是Filotimo__🌈。很高兴与大家相识,希望我的博客能对你有所帮助。 💡本文由Filotimo__✍️原创,首发于CSDN📚。 📣如需转载,请事先与我联

    2024年01月18日
    浏览(31)
  • 数据结构上机实验——栈和队列的实现、栈和队列的应用、进制转换、约瑟夫环问题

      1.利用栈的基本操作实现将任意一个十进制整数转化为R进制整数。   2.利用循环队列实现.约瑟夫环问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到k的那个人出圈;他的下一个人又从1开始报数,数到k的那个人出圈;依

    2024年02月08日
    浏览(31)
  • 力扣环形链表(1)进阶环形链表(2)及环形链表的约瑟夫问题

    为了加深对环形链表的理解和掌握,这两道题是很不错的选择。 这里所说环形链表不是一个圈圈的结构,而是带环链表。 链接:环形链表(1) 注意这里链表的长度 所以要注意链表是否为空 第一种方法,应该是比较容易想到的方法(偷鸡取脚)  遍历链表,将每个节点的

    2024年02月06日
    浏览(23)
  • 数据结构与算法——约瑟夫环

    目录 一、例题引入         # 解题思路         #图例分析         #代码段         #题解小结  二、循环链表         分析:         直接看代码:  三、标记数组         分析:         代码: 四、递归算法          #沿用解释         设有n个人坐在圆桌周围,

    2024年02月08日
    浏览(32)
  • 数据结构实验1约瑟夫环

    刚开始m值为20 循环链表

    2024年02月08日
    浏览(35)
  • 约瑟夫环问题解决

    单链表 实现 错例 在使用malloc函数开辟的空间中,不要进行指针的移动, 因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配 循环链表 单独创建 逐节点创建 约瑟夫环问题 实现方式一: 实现方式二: 删除节点并建立新链表 实现

    2024年02月02日
    浏览(28)
  • 【数据结构与算法】约瑟夫环(C/C++)

    约瑟夫问题的一种描述是:编号为1,2,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。一开始任选一个正整数作为报数上限值m,从第一个人开始。按顺时针方向自1开始顺序报数,报到m时停止报数。报m的人出列,将他的密码作为新的m值,从他在顺时针方向上

    2024年02月12日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包