(C语言)用单向循环链表实现约瑟夫环以及单向循环链表的基础功能

这篇文章描述了一个C语言实现的单向循环链表,包括创建、头插、遍历、尾插、位置插入、判断空、头删、尾删、位置删除、查询和更新数据等基本操作。此外,还实现了约瑟夫问题的解决方案,即在环形链表中按特定规则删除节点直至链表为空。
摘要由CSDN通过智能技术生成

1.创建单向循环链表
2.单向循环链表的头插
3.单向循环链表的遍历
4.单向循环链表的尾插
5.单向循环链表的位置插入
6.单向循环链表判空
7.单向循环链表头删
8.单向循环链表尾删
9.单向循环链表位置删
10.单向循环链根据位置查询数据
11.单向循环链根据位置更新数据
12.1到n个人围圈而坐,约定序号k(1<= k <= n)的人开始计数,数到m的那个人出列,他下一位又从1开始计数,数到m的那个人又出列,以此类推,知道所有人出列为止。

looplink.h

#ifndef __LOOPLIST_H__
#define __LOOPLIST_H__



#include <stdio.h>
#include <stdlib.h>

#define datatype int 
typedef struct node
{
    datatype data;
     struct node *next;
}looplist_t;

/*
*功能:创建单向循环链表
*参数:
*	@:空
*返回值: looplist_t *h
*/
looplist_t* LoopListCreate(void);

/*
*功能:单向循环链表的头插
*参数:
*	@:h:传入链表头节点
   data:插入的数据
*返回值:成功返回值 0 失败返回值 -1
*/
int LoopListInserHead(looplist_t *h, datatype data);

/*
*功能:单向循环链表的遍历
*参数:
*	@:h:传入链表头节点
*返回值:无
*/
void LoopListShow(looplist_t *h);

/*
*功能:单向循环链表的尾插
*参数:
*	@:h:传入链表头节点
   data:插入的数据
*返回值:成功返回值 0 失败返回值 -2
*/
int LoopListInserTail(looplist_t *h, datatype data);

/*
*功能:单向循环链表的位置插入
*参数:
*	@:h:传入链表头节点
    top:插入的位置
   data:插入的数据
*返回值:成功返回值 0 失败返回值 -3
*/
int LoopListInserBytop(looplist_t *h, int top, datatype data);

/*
*功能:单向循环链表判空
*参数:
*	@:h:传入链表头节点
*返回值:链表为空返回 1 非空 0 
*/
int LoopListIsEmpty(looplist_t *h);

/*
*功能:单向循环链表头删
*参数:
*	@:h:传入链表头节点
*返回值:成功返回值 0 失败返回值 -4
*/
int LoopListDeleteHead(looplist_t *h);

/*
*功能:单向循环链表尾删
*参数:
*	@:h:传入链表头节点
*返回值:成功返回值 0 失败返回值 -5
*/
int LoopListDeleteTail(looplist_t *h);

/*
*功能:单向循环链表位置删
*参数:
*	@:h:传入链表头节点
    top:插入的位置
   data:插入的数据
*返回值:成功返回值 0 失败返回值 -6
*/
int LoopListDeleteBytop(looplist_t *h, int top);

/*
*功能:单向循环链根据位置查询数据
*参数:
*	@:h:链表头节点
    top:查询的位置
*返回值:成功返回 0 失败返回 -7
*/
int LoopListSearchDataByTop(looplist_t *h, int top);

/*
*功能:单向循环链根据位置更新数据
*参数:
*	@:h:链表头节点
    top:查询的位置
   data:插入数据
*返回值:成功返回 0 失败返回 -8
*/
int LoopListUpdataByTop(looplist_t *h,datatype data, int top);

/*
*功能:1到n个人围圈而坐,约定序号k(1<= k <= n)的人开始计数,数到m的那个人出列,他下一位
    又从1开始计数,数到m的那个人又出列,以此类推,知道所有人出列为止。
*参数:
*	@:n:总人数
      k:开始计数的人
      m:第m位出列
*返回值:
*/
void joseph(int n,int k,int m);



#endif

looplist.c

#include "looplist.h"

looplist_t* LoopListCreate(void)
{
    looplist_t *h;
    h = (looplist_t *)malloc(sizeof(*h));
    if( h == NULL ) {
        printf("%s h malloc is fail\n",__func__);
        return NULL;
    }
    h->data = 0;
    h->next = h;
    return h;
}

int LoopListInserHead(looplist_t *h, datatype data)
{
    looplist_t *temp;
    temp = (looplist_t *)malloc(sizeof(*temp));
    if(temp == NULL) {
         printf("%s temp malloc is fail\n",__func__);
        return -1;
    }
    temp->data = data;
    temp->next = h->next;
    h->next = temp;
    return 0;
}

void LoopListShow(looplist_t *h) 
{
    looplist_t *temp = h;
    while (h->next != temp)
    {
        h = h->next;
        printf("-%d",h->data); 
    }
    printf("-\n");
}

int LoopListInserTail(looplist_t *h, datatype data)
{
    looplist_t *temp, *temp_h;
    temp = (looplist_t *)malloc(sizeof(*temp));
    if(temp == NULL) {
         printf("%s temp malloc is fail\n",__func__);
        return -2;
    }
    temp_h = h;
    while (h->next != temp_h)
    {
        h = h->next;
    }
    temp->data = data;
    temp->next = h->next;
    h->next = temp;
    return 0;
}

int LoopListInserBytop(looplist_t *h, int top, datatype data)
{
    looplist_t *temp,*temp_h = h;
    temp = (looplist_t *)malloc(sizeof(*temp));
    if(temp == NULL) {
         printf("%s temp malloc is fail\n",__func__);
        return -3;
    }
    if( top < 0 ) {
         printf("%s top 小于 0\n",__func__);
         return -3;
    }
    int num = 0;
    while (top--)
    {
        h = h->next;
        if( h == temp_h) {
            printf("%s top超出范围\n",__func__);
            return -3;
        }
    }
    temp->data = data;
    temp->next = h->next;
    h->next = temp;
    return 0;
}

int LoopListIsEmpty(looplist_t *h) 
{
    return h->next == h;
}

int LoopListDeleteHead(looplist_t *h)
{
    if(LoopListIsEmpty(h)) {
        printf("%s 删除失败该链表为空!\n",__func__);
        return -4;
    }
    looplist_t *temp;
    temp = h->next;
    h->next = h->next->next;
    free(temp);
    temp = NULL;
    return 0;
}

int LoopListDeleteTail(looplist_t *h)
{
    if(LoopListIsEmpty(h)) {
        printf("%s 删除失败该链表为空!\n",__func__);
        return -5;
    }
    looplist_t *temp,*temp_h = h;
    while (h->next->next != temp_h)
    {
        h = h->next;
    }
    temp = h->next;
    h->next = temp_h;
    free(temp);
    temp = NULL;
    return 0;
}

int LoopListDeleteBytop(looplist_t *h, int top)
{
    if(LoopListIsEmpty(h)) {
        printf("%s 删除失败该链表为空!\n",__func__);
        return -6;
    }
    if( top < 0 ) {
         printf("%s top 小于 0\n",__func__);
         return -6;
    }
    looplist_t *temp,*temp_h = h;
    while (top--)
    {
        h = h->next;
        if(h->next == temp_h) {
            printf("%s top超出范围\n",__func__);
            return -6;
        }
    }
    temp = h->next;
    h->next = h->next->next;
    free(temp);
    temp = NULL;
    return 0;
}

int LoopListSearchDataByTop(looplist_t *h, int top)
{
    if(LoopListIsEmpty(h)) {
        printf("%s 查询失败该链表为空!\n",__func__);
        return -7;
    }
    if( top < 0 ) {
         printf("%s top 小于 0\n",__func__);
         return -7;
    }
    int num = top;
    looplist_t *temp,*temp_h = h;
    while (top--)
    {
        h = h->next;
        if(h->next == temp_h) {
            printf("%s top超出范围\n",__func__);
            return -7;
        }
    }
    printf("第%d个节点的值为%d\n",num,h->next->data);
    return 0;
}

int LoopListUpdataByTop(looplist_t *h,datatype data, int top)
{
    if(LoopListIsEmpty(h)) {
        printf("%s 查询失败该链表为空!\n",__func__);
        return -8;
    }
    if( top < 0 ) {
         printf("%s top 小于 0\n",__func__);
         return -8;
    }
    int num = top,num2;
    looplist_t *temp,*temp_h = h;
    while (top--)
    {
        h = h->next;
        if(h->next == temp_h){
            printf("%s top超出范围\n",__func__);
            return -8;
        }
    }
    num2 = h->next->data;
    h->next->data = data;
    printf("第%d位数据已由%d更新为%d!\n",num,num2,data);
    return 0;
}

joseph.c

#include "looplist.h"


void joseph(int n,int k,int m)
{
    if( k < 1 || k > n) {
        printf("%s k的值应在1到n之间(包含1,n)\n",__func__);
    }
    looplist_t *h,*temp,*temp_h;
    int num = m;
    h = (looplist_t *)malloc(sizeof(*h));
    if( h == NULL) {
        printf("%s malloc is fail!\n",__func__);
        return;
    }
    h->data = 1;
    h->next = h;

    for(int i = 2; i <= n;i++ )
        LoopListInserTail(h,i);
    // LoopListShow(h);
    while (--k) {
        h = h->next;
    }
    temp_h = h;
    if(m == 1){
        while (h->next != temp_h)
        {
            printf("%d ",h->data);
            h = h->next;
        }
        printf("%d\n",h->data);
        return;
    }
    while (1)
    {
        int num = m - 1;
        while(--num) {
            h = h->next;
            if(h == h->next) {
                printf("%d\n",h->data);
                return;
            }
        }
        printf("%d ",h->next->data);
        temp = h->next;
        h->next = h->next->next;
        free(temp);
        temp = NULL;
        h = h->next;
    }
}

main.c

#include "looplist.h"

int main(int argc,const char * argv[])
{
    looplist_t *h;
    // h = LoopListCreate();
    // printf("%d\n" ,LoopListIsEmpty(h));
    // LoopListInserHead(h,233);
    // LoopListInserHead(h,222);
    // LoopListInserTail(h,444);
    // LoopListInserHead(h,333);
    // LoopListInserTail(h,555);
    // LoopListInserBytop(h,0,256);
    // LoopListInserBytop(h,3,256);
    // LoopListShow(h);
    // LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    //  LoopListDeleteHead(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteTail(h);
    // LoopListShow(h);
    // LoopListDeleteBytop(h,0);
    // LoopListShow(h);
    // LoopListDeleteBytop(h,5);
    // LoopListShow(h);
    // LoopListDeleteBytop(h,3);
    // LoopListShow(h);
    // LoopListDeleteBytop(h,5);
    // LoopListShow(h);
    // LoopListSearchDataByTop(h,0);
    // LoopListUpdataByTop(h,234,0);
    // LoopListShow(h);
    // LoopListSearchDataByTop(h,1);
    // LoopListUpdataByTop(h,234,1);
    // LoopListShow(h);
    // LoopListSearchDataByTop(h,6);
    // LoopListUpdataByTop(h,234,6);
    // LoopListShow(h);
    // LoopListSearchDataByTop(h,7);
    joseph(8,3,4);
    return 0;
}

运行效果:
基础功能已测试都可使用,就不一一截图了。
只附约瑟夫问题运行截图一张:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值