汉扬编程 C语言入门 一个约瑟夫问题求解,高手请进!!!

一个约瑟夫问题求解,高手请进!!!

  #include

#include

#define MAX_NODE_NUM 100

typedef struct NodeType

{

int id;

int cipher;

struct NodeType *next;

} NodeType;

static int CreateRandom(); /* 产生随机数作为密码 */

static void CreateList(NodeType **, const int); /* 创建单向循环链表 */

static void StartJoseph(NodeType **, int); /* 运行\”约瑟夫环\”问题 */

static void PrintList(const NodeType *); /* 打印循环链表 */

static NodeType *GetNode(const int, const int); /* 得到一个结点 */

static unsigned EmptyList(const NodeType *); /* 测试链表是否为空, 空为TRUE,非空为FALSE */

int main(void)

{

int n, m;

NodeType *pHead = NULL;

while (1)

{

printf(\”请输入人数n(最多%d个): \”, MAX_NODE_NUM);

scanf(\”%d\”, &n);

printf(\”和初始密码m: \”);

scanf(\”%d\”, &m);

if (n > MAX_NODE_NUM)

{

printf(\”人数太多,请重新输入!\\n\”);

continue;

}

else

break;

}

CreateList(&pHead, n);

printf(\”\\n———–循环链表初始话打印————-\\n\”);

PrntList(pHead);

printf(\”\\n———–删除出队情况打印 ————-\\n\”);

StartJoseph(&pHead, m);

}

static int CreateRandom();

{

int num;

srand((int)time(0));

num=1+(int) rand()%MAX_NODE_NUM;

return num;

}

static void CreateList(NodeType **ppHead, const int n)

{

int i, iCipher;

NodeType *pNew, *pCur;

for (i = 1; i next = *ppHead;

}

else

{

pNew->next = pCur->next;

pCur->next = pNew;

pCur = pNew;

}

}

printf(\”完成单向循环链表的创建!\\n\”);

}

static void StartJoseph(NodeType **ppHead, int iCipher)

{

int iCounter, iFlag = 1;

NodeType *pPrv, *pCur, *pDel;

pPrv = pCur = *ppHead;

/* 将pPrv初始为指向尾结点,为删除作好准备 */

while (pPrv->next != *ppHead)

pPrv = pPrv->next;

while (iFlag)

{

for (iCounter = 1; iCounter next;

}

if (pPrv == pCur)

iFlag = 0;

pDel = pCur; /* 删除pCur指向的结点,即有人出列 */

pPrv->next = pCur->next;

pCur = pCur->next;

iCipher = pDel->cipher;

printf(\”第%d个人出列, 密码: %d\\n\”, pDel->id, pDel->cipher);

free(pDel);

}

*ppHead = NULL;

}

static void PrintList(const NodeType *pHead)

{

const NodeType *pCur = pHead;

if (EmptyList(pHead))

return;

do

{

printf(\”第%d个人, 密码: %d\\n\”, pCur->id, pCur->cipher);

pCur = pCur->next;

} while (pCur != pHead);

}

static NodeType *GetNode(const int iId, const int iCipher)

{

NodeType *pNew;

pNew = (NodeType *)malloc(sizeof(NodeType));

if(!pNew)

{

printf(\”Error, the memory is not enough!\\n\”);

exit(-1);

}

pNew->id = iId;

pNew->cipher = iCipher;

pNew->next = NULL;

return pNew;

}

static unsigned EmptyList(const NodeType *pHead)

{

if(!pHead)

{

return TRUE;

}

return FALSE;

}


  

本文来自网络,不代表汉扬编程立场,转载请注明出处:http://www.hyzlch.com/cjia/7021.html

(吉他初学者应该买什么样的吉他)初学者用什么编C语言比较好

C语言指针访问数组基础知识,理解各种符号的意义

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

返回顶部