#include#include typedef struct Node { int date; struct Node *Next; } NODE, *PNODE; PNODE creat_list(); void shiyan1(PNODE pHead, int k, int m);//k是位置,m是报的数 int main() { PNODE p = creat_list(); int k; int m; printf("请输入开始位置:"); scanf("%d", &k); printf("请输入m:"); scanf("%d", &m); shiyan1(p, k, m); } PNODE creat_list() {//单向循环链表的创建。 int n; int val; int i = 1; PNODE pHead = (PNODE) malloc(sizeof(NODE)); pHead->date = 0; PNODE pTail = pHead; pTail->Next = NULL; printf("请输入您所要创建链表的长度:"); scanf("%d", &n); while (i <= n) { PNODE pNew = (PNODE) malloc(sizeof(NODE)); pNew->date = i; pNew->Next = NULL; pTail->Next = pNew; pTail = pNew; i++; } pTail->Next = pHead->Next; return pHead->Next;//返回首结点 } void shiyan1(PNODE pNode, int k, int m) { for (int i = 1; i < k; i++) {//此循环是找开始位置的结点 pNode = pNode->Next; } if (m != 1) { while (pNode->Next != pNode) {//如果该结点不指向他自己,开始循环 PNODE pTemp = pNode;//定义一个指针寻找报到m的结点(被删除结点)的前一个 for (int i = 1; i < m - 1; i++) {//寻找被删除元素的前一个。 pTemp = pTemp->Next; } PNODE pStorage = pTemp->Next; pTemp->Next = pStorage->Next; printf("%d ", pStorage->date); free(pStorage); pNode = pTemp->Next; } if (pNode->Next == pNode) { printf("%d", pNode->date); } } if (m == 1) { while (pNode->Next != pNode) {//如果该结点不指向他自己,开始循环 PNODE pTemp = pNode;//定义一个指针寻找报到m的结点(被删除结点)的前一个 for (int i = 1; i < m - 1; i++) {//寻找被删除元素的前一个。 pTemp = pTemp->Next; } while (pTemp->Next != pNode) { pTemp = pTemp->Next; } PNODE pStorage = pTemp->Next; pTemp->Next = pStorage->Next; printf("%d ", pStorage->date); free(pStorage); pNode = pTemp->Next; } if (pNode->Next == pNode) { printf("%d", pNode->date); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)