程序代码:
#include<stdio.h> #include<stdlib.h> #define Status int #define OK 1 struct Node { int data; struct Node *next; }; typedef struct Node ListNode; typedef struct Node *LinkList; LinkList CreatCyclelist(int n)//创建一个不带头节点的循环单链表 { LinkList head = NULL; int e; int i; ListNode *p, *q; q = NULL; for (i = 1; i <= n; i++) { printf("请输入元素:\n"); scanf_s("%d",&e); if (i == 1) { head = (LinkList)malloc(sizeof(ListNode)); head->data = e; head->next = NULL; q = head; } else { p = (LinkList)malloc(sizeof(ListNode)); p->data = e; p->next = NULL; q->next = p; q=q->next; } } if (q != NULL) q->next = head; int m,counter=2; ListNode *s; ListNode *Q; s = Q = NULL; s = head; printf("请输入一个正整数作为报数上限值"); scanf_s("%d",&m); if (m == 1) { q->next = head; free(head); } else { m=0; while(true) { s=s->next; if(m%counter==0) { Q=s->next; s->next=Q->next; free(Q); } m++; if(s->next==s) break; } } return s; } void DisplayCycleList(LinkList head) { ListNode *p; p = head; if (p == NULL) { printf("该链表是空表"); return; } while (p->next != head) { printf("%d", p->data); p = p->next; } printf("%4d", p->data); } main() { int n; LinkList h; // printf("请输入顺序链表的表尾值:"); printf("请输入顺序链表的的长度:"); scanf_s("%d",&n); h = CreatCyclelist(n); DisplayCycleList(h); return 0; }
剑栈风樯各苦辛,别时冰雪到时春