【C语言】 约瑟夫环,循环链表实现

1、循环链表实现约瑟夫环,每次经过特定步数删除一个元素

cs 复制代码
//looplist.h
#ifndef LOOPLIST_H
#define LOOPLIST_H
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef int datatype;

typedef struct Node
{
    union 
    {
        int len;
        datatype data;
    };
    struct Node *next;
}Node,*NodePtr;

//创建循环链表
NodePtr link_create();

//判空
int link_empty(NodePtr L);

//链表申请空间封装节点
NodePtr apply_node(datatype e);


//按位置进行查找
NodePtr list_search_pos(NodePtr L, int pos);

//头插
int link_insert_tail(NodePtr L,datatype e);

//遍历链表
int link_show(NodePtr L);

//约瑟夫
int link_yue(NodePtr L,int flag);
#endif
cs 复制代码
//looplist.c
#include"looplist.h"


//创建循环链表
NodePtr link_create()
{
    NodePtr L = (NodePtr)malloc(sizeof(Node));
    if(NULL == L)
    {
        printf("链表创建失败\n");
        return NULL;
    }
    L->len = 0;
    L->next = L;
    printf("创建链表成功\n");
    return L;
}

//判空
int link_empty(NodePtr L)
{
    return L->next == L;
}

//链表申请空间封装节点
NodePtr apply_node(datatype e)
{
    //堆区申请一个节点的空间
    NodePtr p = (NodePtr)malloc(sizeof(Node));
    if(NULL == p)
    {
        printf("申请失败\n");
        return NULL;
    }

    //给节点赋值
    p->data = e;
    p->next = NULL;

    return p;
}



//按位置进行查找
NodePtr list_search_pos(NodePtr L, int pos)
{
    if(NULL == L || pos < 0 || pos > L->len)
    {
        printf("查找失败\n");
        return NULL;
    }
    NodePtr q = L;
    for (int  i = 0; i < pos; i++)
    {
        q = q->next;
    }
    return q;
}



//尾插
int link_insert_tail(NodePtr L,datatype e)
{   
    if(NULL == L)
    {
        printf("插入失败\n");
        return -1;
    }
    NodePtr q = list_search_pos(L,L->len);
    NodePtr p = apply_node(e);
    p->next = q->next;
    q->next = p;
    L->len++;
    printf("插入成功\n");
    return 0;
}


//遍历链表
int link_show(NodePtr L)
{
    if(NULL == L || link_empty(L))
    {
        printf("遍历失败\n");
        return -1;
    }
    NodePtr q = L->next;
    while(q != L)
    {
        printf("%d\t",q->data);
        q = q->next;
    }
    printf("\n");
}



//约瑟夫
int link_yue(NodePtr L, int flag) {
    if (L == NULL || flag <= 0 || link_empty(L)) {
        printf("输入的参数有误或链表为空。\n");
        return -1;
    }

    NodePtr prev = L; 
    NodePtr curr = L->next; 

    while (L->len > 1) 
    { 
        for (int count = 0; count < flag; count++) 
        {
            prev = curr; 
            curr = curr->next; 
        }
       
        printf("删除节点的值为: %d\n", curr->data); 
        prev->next = curr->next; 
        NodePtr temp = curr; 
        curr = curr->next; 
        L->len--; 
        free(temp); 
    }

    // 输出最后剩下的节点数据
    printf("最后剩下的节点数据是:%d\n", prev->data);
    return 0;
}
cs 复制代码
//main.c
#include"looplist.h"
int main(int argc, char const *argv[])
{
    NodePtr L = link_create();
    int flag = 0,n = 0,sum = 0;
    printf("请输入你想输入的个数:");
    scanf("%d",&n);

    //循环插入值
    for(int i = 0;i < n;i++)
    {
        printf("请输入第%d个值:",i+1);
        scanf("%d",&sum);
        link_insert_tail(L,sum);
    }


    printf("当前链表中的元素为:");
    link_show(L);

    printf("请输入你想走多少步移除一个数:");
    scanf("%d",&flag);
    getchar();

    //调用约瑟夫环函数
    link_yue(L,flag);
    return 0;
}

输出结果如下:

相关推荐
少许极端7 小时前
算法奇妙屋(二十八)-递归、回溯与剪枝的综合问题 1
java·算法·深度优先·剪枝·回溯·递归
仰泳的熊猫7 小时前
题目1453:蓝桥杯历届试题-翻硬币
数据结构·c++·算法·蓝桥杯
rainbow68897 小时前
C++STL list容器模拟实现详解
开发语言·c++·list
云中飞鸿7 小时前
VS编写QT程序,如何向linux中移植?
linux·开发语言·qt
唐梓航-求职中7 小时前
技术-算法-leetcode-1606. 找到处理最多请求的服务器(易懂版)
服务器·算法·leetcode
Boop_wu7 小时前
简单介绍 JSON
java·开发语言
啊阿狸不会拉杆7 小时前
《机器学习导论》第 10 章-线性判别式
人工智能·python·算法·机器学习·numpy·lda·线性判别式
超龄超能程序猿7 小时前
Python 反射入门实践
开发语言·python
Katecat996637 小时前
Faster R-CNN在药片边缘缺陷检测中的应用_1
开发语言·cnn
会叫的恐龙7 小时前
C++ 核心知识点汇总(第11日)(排序算法)
c++·算法·排序算法