题目:
?代码: ?
#include<iostream>
using namespace std;
typedef struct SList
{
int data;
struct SList* next;
}SL;
void SLInit(SL** ps)
{
*ps = (SL*)malloc(sizeof(SL));
(*ps)->next = NULL;
}//开辟头结点
void SLCreate(SL** ps,int n)
{
SL* head = *ps;
SL* cur = *ps;
for (int i = 1; i <= n; i++)
{
SL *newnode = (SL*)malloc(sizeof(SL));
newnode->data = i;
cur->next = newnode;
cur = newnode;
}
cur->next = head->next;//构成循环链表
}
int main()
{
SL* q = NULL;
SLInit(&q);
int n = 0;
cin >> n;
SLCreate(&q, n);
int k = 0;
cin >> k;
while (n != 1)//从1开始循环直到n结束
{
int i = 1;//从1开始循环直到k结束
while (i != k)
{
q = q->next;
i++;
}
q->next = q->next->next;//将第k个元素出队列
n--;
}
printf("%d", q->data);//打印最后一个剩下的数据
return 0;
}
|