约瑟夫环问题

写了个约瑟夫环,未有来得及参考别人程序,刚刚接触链表,循环链表也是自己构思出的,可以定义实现各个方面存在很大问题(如:起初使用后插入数据的方法就可以方面数据的加入,实现节点查找删除的过程也不够灵活,后续的数据输出还存在很大问题),关于约瑟夫环之后会陆续优化代码。

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


typedef unsigned char uchar;
typedef struct node
{
uchar num;
struct node *next;
}Node;


Node *head = NULL;
Node *end = NULL;


void SchNode();
void CreatCirLink();
void CreatNode();
void CreatMem(uchar a);
void DispNode();


int main()
{
printf("ok!");
CreatCirLink();
DispNode();
//printf("%d\n",__LINE__);
SchNode();
Node *ptr = head;
printf("%d\n",ptr->num);
}


void CreatCirLink() //创建循环链表
{
int a;
CreatNode();
scanf("%d",&a);
//printf("%d\n",__LINE__);
CreatMem(a);

}
void CreatNode()
{
head = (Node *)malloc(sizeof(Node));
head->next = NULL;
end = head;
}


void CreatMem(uchar a)
{
Node *ptr = NULL;
Node *temp = NULL;
uchar i = 1;
while(i != a)
{
ptr = (Node *)malloc(sizeof(Node));
ptr->next = head;
head = ptr;
i++;
}
i = 1;
ptr = head;
//printf("%d\n",__LINE__);
do
{
ptr->num = i;
ptr = ptr->next;
//
i++;
}while(i != (a+1));
end->next = head;
//printf("%d\n",__LINE__);
}


void SchNode()
{
Node *ptr,*temp;
int a = 2;
ptr = head->next;
temp = head;
//printf("%d\n",ptr->num);
//printf("%d\n",head->num);
while(ptr->next != ptr)
{

if(a % 3 == 0)
{
//printf("%d\n",ptr->num);
if(ptr == head)
{
head = head->next;
}
temp->next = ptr->next;
free(ptr);
ptr = temp->next;


}

else
{
//printf("%d\n",__LINE__);
temp = ptr;
ptr = ptr->next;
}
a++;
}
}




void DispNode()
{
Node *ptr;
ptr = head;
//printf("%d\n",__LINE__);
printf("%d\n",ptr->num);

do
{
ptr = ptr->next;
printf("%d\n",ptr->num);
}while(ptr != end);
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值