按照1-8顺寻存储,起始位置为3,数到4的人出列。
#include<iostream>
using namespace std;
typedef struct node
{int num;struct node* next;
}Node;
int main()
{int n = 8, k = 3, m = 4;Node*h = (Node*)malloc(sizeof(Node));h->num = 1;h->next = NULL;Node*p = NULL; //存储临时结点Node*q = h; //q负责串连结点组成链表int i;for (i = 2; i <= 8; i++){p = (Node*)malloc(sizeof(Node));p->num = i;p->next = NULL;q->next = p; //存储新生成的结点q = q->next; //当前存储结点后移,最后一个的q next始终为空}q->next = h;//定位初始位置for (i = 1; i < k; i++){h = h->next;}while(h->next!=h){//要删除结点的前一个结点for (i = 1; i < m - 1; i++){h = h->next;}p = h->next;cout << p->num << endl;h->next = p->next;free(p);h = h->next;//因为删除了一个所以要继续向下走一个}cout << p->num << endl; //最后一个结点free(h);system("pause");return 0;
}