c语言实验报告约瑟夫环,C语言约瑟夫环的实现

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

实现代码

#include

#include

#include

struct Node;

typedef struct Node *PtrToNode;

typedef PtrToNode List;

typedef PtrToNode Position;

typedef int ElementType;

struct Node{

Position previous;

ElementType element;

Position next;

};

void InitList(List l, int iNum);

void OutPeople(List l, int iCount, int iPeopleNum);

void printfList(List l, int i);

int main(int argc, char **argv){

int iPeopleNum, iDeliverCount;

List l = (List)malloc(sizeof(struct Node));

if(argc != 3){

perror("input parameters error");

exit(2);

}

iPeopleNum = atoi(argv[1]);

iDeliverCount = atoi(argv[2]);

InitList(l, iPeopleNum);

printfList(l, iPeopleNum);

OutPeople(l, iDeliverCount, iPeopleNum);

printfList(l, 5);

return 0;

}

void InitList(List l, int iNum){

List tmp = l;

Position pos = tmp, BeginPos, LastPos;

PtrToNode node;

int i;

pos->previous = NULL;

pos->element = 1;

pos->next = NULL;

for(i = 1; i < iNum; i++){

node = (PtrToNode)malloc(sizeof(struct Node));

node->element = i+1;

node->next = NULL;

node->previous = pos;

pos->next = node;

pos = pos->next;

}

BeginPos = tmp;

pos = tmp;

while(pos->next != NULL){

pos = pos->next;

}

LastPos = pos;

LastPos->next = BeginPos;

BeginPos->previous = LastPos;

}

void OutPeople(List l, int iCount, int iPeopleNum){

List tmp = l;

Position pos = tmp;

Position PreviousPos, NextPos;

int i, j;

for(j = 0; j < iPeopleNum - 1; j++){

for(i = 0; i <= iCount; i++){

PreviousPos = pos->previous;

NextPos = pos->next;

if(i == iCount){

PreviousPos->next = NextPos;

NextPos->previous = PreviousPos;

printf("out element %d\n", pos->element);

free(pos);

pos = NextPos;

}else{

pos = pos->next;

}

}

}

}

void printfList(List l, int n){

List tmp = l;

Position pos = tmp;

printf("has %d elements\n", n);

int i;

printf("element: %d, next element: %d\n", pos->element, pos->next->element);

for(i = 0; i < n; i++){

printf("%d ", pos->element);

pos = pos->next;

}

printf("\n");

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值