NO.1 约瑟夫环问题
采用单向循环表的数据结构,即将链表的尾元素的指针指向链首元素。每个结点除了指针域外,还有两个域分别存放 id 和 password 。
约瑟夫环(约瑟夫问题)是一个数学的应用问题:
已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。通常解决这类问题时我们把编号从0~n-1,最后[1]结果+1即为原问题的解。
解决问题的基本步骤:
(1)建立n个结点(无头结点)的单向循环链表;
(2)从链表第一个结点起循环计数寻找第m个结点;
(3)输出该结点的id值;
(4)根据m值不断从链表中删除结点,直到链表为空。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAX 100 4 5 typedef struct NodeType //自定义结构体类型 6 { 7 int id; 8 int password; 9 struct NodeType *next; //用于指向下一个结点的指针 10 }NodeType; 11 12 void CreatList(NodeType **, int); //创建单向循环链表 13 NodeType *GetNode(int, int); //得到一个结点 14 void PrintList(NodeType *); //打印循环链表 15 int IsEmptyList(NodeType *); //判空 16 void JosephusOperate(NodeType **, int); //运行环求解 17 18 int main(void) 19 { 20 int n = 0; 21 int m = 0; 22 NodeType *pHead = NULL; 23 do 24 { 25 if (n>MAX) 26 { 27 printf("人数太多,请重新输入!\n"); 28 } 29 printf("请输入人数n(最多%d个):", MAX); 30 scanf("%d", &n); 31 } while (n>MAX); 32 printf("请输入初始密码m:"); 33 scanf("%d", &m); 34 CreatList(&pHead, n); //创建单向循环链表 35 printf("\n----------打印循环链表---------\n"); 36 PrintList(pHead); //打印链表 37 printf("\n----------打印出队情况---------\n"); 38 JosephusOperate(&pHead, m); //运行 39 return 1; 40 } 41 42 void CreatList(NodeType **ppHead, int n) //创建有n个结点的循环链表pphead 43 { 44 int i = 0; 45 int iPassword = 0; 46 NodeType *pNew = NULL; 47 NodeType *pCur = NULL; 48 for (i = 1; i <= n; i++) 49 { 50 printf("输入第%d个人的密码:",i); 51 scanf("%d", &iPassword); 52 pNew = GetNode(i, iPassword); 53 if (*ppHead == NULL) 54 { 55 *ppHead = pCur = pNew; 56 pCur->next = *ppHead; 57 } 58 else 59 { 60 pNew->next = pCur->next; 61 pCur->next = pNew; 62 pCur = pNew; 63 } 64 } 65 printf("完成单向循环链表的创建!\n"); 66 } 67 68 NodeType *GetNode(int iId, int iPassword) 69 { 70 NodeType *pNew = NULL; 71 pNew = (NodeType *)malloc(sizeof(NodeType)); 72 if (!pNew) 73 { 74 printf("Error, the memory is not enough!\n"); 75 exit(-1); 76 } 77 pNew->id = iId; 78 pNew->password = iPassword; 79 pNew->next = NULL; 80 return pNew; 81 } 82 83 void PrintList(NodeType *pHead) 84 { 85 NodeType *pCur = pHead; 86 if (!IsEmptyList(pHead)) 87 { 88 printf("--ID-- --PASSWORD--\n"); 89 do 90 { 91 printf("%3d %7d\n", pCur->id, pCur->password); 92 pCur = pCur->next; 93 } while (pCur!=pHead); 94 } 95 } 96 97 int IsEmptyList(NodeType *pHead) 98 { 99 if (!pHead) 100 { 101 printf("The list is empty!\n"); 102 return 1; 103 } 104 return 0; 105 } 106 107 void JosephusOperate(NodeType **ppHead, int iPassword) 108 { 109 int iCounter = 0; 110 int iFlag = 1; 111 NodeType *pCur = NULL; 112 NodeType *pPrv = NULL; 113 NodeType *pDel = NULL; 114 pPrv = pCur = *ppHead; 115 while (pPrv->next != *ppHead) 116 { 117 pPrv = pPrv->next; 118 } 119 while (iFlag) 120 { 121 for (iCounter = 1; iCounter < iPassword; iCounter++) 122 { 123 pPrv = pCur; 124 pCur = pCur->next; 125 } 126 if (pPrv==pCur) 127 { 128 iFlag = 0; 129 } 130 pDel = pCur; 131 pPrv->next = pCur->next; 132 pCur = pCur->next; 133 iPassword = pDel->password; 134 printf("第%d个人出列(密码:%d)\n", pDel->id, pDel->password); 135 free(pDel); 136 } 137 *ppHead = NULL; 138 getchar(); 139 }
2.一元多项式加减计算
1 #include<stdio.h> 2 #include<malloc.h> 3 4 typedef struct Polynomail 5 { 6 float coef; 7 int expn; 8 struct Polynomail *next; 9 }Polyn; 10 11 Polyn *creatPoly(void); 12 void printPoly(Polyn *P); 13 Polyn *addPoly(Polyn *pa, Polyn *pb); 14 Polyn *subtractPoly(Polyn *pa, Polyn *pb); 15 16 Polyn *subtractPoly(Polyn *pa, Polyn *pb) 17 { 18 Polyn *h = pb; 19 Polyn *p = pb->next; 20 Polyn *pd; 21 while (p) 22 { 23 p->coef *= -1; 24 p = p->next; 25 } 26 pd = addPoly(pa, h); 27 for (p = h->next; p; p = p->next) 28 p->coef *= -1; 29 return pd; 30 } 31 32 Polyn *addPoly(Polyn *pa, Polyn *pb) 33 { 34 Polyn *qa = pa->next; 35 Polyn *qb = pb->next; 36 Polyn *headc, *pc, *qc; 37 38 pc = (Polyn *)malloc(sizeof(Polyn)); 39 pc->next = NULL; 40 headc = pc; 41 while (qa != NULL && qb != NULL) 42 { 43 qc = (Polyn *)malloc(sizeof(Polyn)); 44 if (qa->expn < qb->expn) 45 { 46 qc->coef = qa->coef; 47 qc->expn = qa->expn; 48 qa = qa->next; 49 } 50 else if (qa->expn == qb->expn) 51 { 52 qc->coef = qa->coef + qb->coef; 53 qc->expn = qa->expn; 54 qa = qa->next; 55 qb = qb->next; 56 } 57 else 58 { 59 qc->coef = qb->coef; 60 qc->expn = qb->expn; 61 qb = qb->next; 62 } 63 if (qc->coef != 0) 64 { 65 qc->next = pc->next; 66 pc->next = qc; 67 pc = qc; 68 } 69 else 70 free(qc); 71 } 72 while (qa != NULL) 73 { 74 qc = (Polyn *)malloc(sizeof(Polyn)); 75 qc->coef = qa->coef; 76 qc->expn = qa->expn; 77 qa = qa->next; 78 qc->next = pc->next; 79 pc->next = qc; 80 pc = qc; 81 } 82 while (qb != NULL) 83 { 84 qc = (Polyn *)malloc(sizeof(Polyn)); 85 qc->coef = qb->coef; 86 qc->expn = qb->expn; 87 qb = qb->next; 88 qc->next = pc->next; 89 pc->next = qc; 90 pc = qc; 91 } 92 return headc; 93 } 94 95 void printPoly(Polyn *P) 96 { 97 Polyn *q = P->next; 98 int flag = 1; 99 if (!q) 100 { 101 putchar('0'); 102 printf("\n"); 103 return; 104 } 105 while (q) 106 { 107 if (q->coef > 0 && flag != 1) 108 putchar('+'); 109 if (q->coef != 1 && q->coef != -1) 110 { 111 printf("%g", q->coef); 112 if (q->expn == 1) 113 putchar('X'); 114 else if (q->expn) 115 printf("X^%d", q->expn); 116 } 117 else 118 { 119 if (q->coef == 1) 120 { 121 if (!q->expn) 122 putchar('1'); 123 else if (q->expn == 1) 124 putchar('X'); 125 else 126 printf("X^%d", q->expn); 127 } 128 if (q->coef == -1) 129 { 130 if (!q->expn) 131 printf("-1"); 132 else if (q->expn == 1) 133 printf("-X"); 134 else 135 printf("-X^%d", q->expn); 136 } 137 } 138 q = q->next; 139 flag++; 140 } 141 printf("\n"); 142 } 143 144 Polyn *creatPoly(void) //建立 145 { 146 Polyn *head, *rear, *s; 147 int c, e; 148 149 head = (Polyn *)malloc(sizeof(Polyn)); 150 rear = head; 151 printf("请输入多项式的系数和指数项(0结束输入)"); 152 scanf("%d%d", &c, &e); 153 while (c != 0) 154 { 155 s = (Polyn *)malloc(sizeof(Polyn)); 156 s->coef = c; 157 s->expn = e; 158 rear->next = s; 159 rear = s; 160 printf("请继续输入多项式的系数和指数项(0结束输入)"); 161 scanf("%d%d", &c, &e); 162 } 163 rear->next = NULL; 164 return head; 165 } 166 167 int main(void) 168 { 169 Polyn *p1, *p2, *p3, *p4; 170 171 printf("多项式1:\n"); 172 p1 = creatPoly(); 173 printf("多项式2:\n"); 174 p2 = creatPoly(); 175 printf("您输入的多项式如下所示:\n"); 176 printPoly(p1); 177 printPoly(p2); 178 p3 = addPoly(p1, p2); 179 p4 = subtractPoly(p1, p2); 180 printf("多项式相加后的结果如下:\n"); 181 printPoly(p3); 182 printf("多项式相减后的结果如下:\n"); 183 printPoly(p4); 184 }