Using the circular chain list, with the take-over operation. (VS2010)
#include <stdio.h>
#include <stdlib.h>
#include "stdafx.h"
#include <iostream>
typedef struct node{
int data;
Node* Next;
}node;
void Creatlist (node*& head, node*& tail, int n) {
if (n<1) {
Head = tail = NULL;
Return
}
Head = new node ();
Head->data = 1;
Head->next = NULL;
node* p = head;
for (int i=2; i<n+1; i++)
{
P->next =new node ();
p = p->next;
P->data = i;
P->next = NULL;
}
tail = p;
Tail next = head;
}
void Print (node*& head) {
node* p = head;
while (P && p->next! = head) {
printf ("%d", p->data);
p = p->next;
}
if (p) {
printf ("%d\n", p->data);
}
}
void Countprint (node*& head, node*& tail, int m) {
node* pre = tail;
node* cur = head;
int cnt = m;
while (cur && cur->next! = cur) {
if (cnt! = 1) {
cnt--;
Pre = cur;
Cur = cur->next;
}else{
printf ("%d", cur->data);
Pre->next =cur->next;
Delete cur;
Cur = pre->next;
CNT = m;
}
}
if (cur) {
printf ("%d", cur->data);
Delete cur;
head = Tail =null;
}
}
int main () {
node* Head;
node* tail;
int m;
int n;
scanf_s ("%d", &n);
scanf_s ("%d", &m);
Creatlist (head, tail, n);
Print (head);
Countprint (HEAD,TAIL,M);
System ("pause");
return 0;
}
(1) User input m,n value, starting from 1 to n sequential number of cycles, each number to M output this value, until the full output. Write the C program.