#include <iostream>using namespace std;typedef struct linknode { int value; struct linknode *next; }linknode,*linklist;linklist createcycle(int total) { int index = 1; linklist prev = NULL,curr = NULL,head = NULL; head = new linknode; head->value = index; prev = head; for(;index < total;) { curr = new linknode; curr->value = ++index; prev->next = curr; prev = curr; } prev->next = head; return head; }void run(int total,int tag) { linklist node = createcycle(total); linklist curr = node,prev = NULL; while (curr == curr->next) { for(int i = 0;i < tag-1;++i) { prev = curr; curr = prev->next; } cout<<curr->next->value<<"->"; curr->next = curr->next->next; delete curr->next; prev = curr; curr = prev->next; } cout<<curr->value<<endl; delete curr; }int main(){ run(41,3); return 0;}
1 回答
Lornsoul
TA贡献1条经验 获得超0个赞
已解决,抱歉问了个傻问题,现代码
void run(int total,int tag)
{
linklist node = createcycle(total);
linklist curr = node,prev = NULL;
while (curr != curr->next)
{
for(int i = 0;i < tag-1;++i)
{
prev = curr;
curr = prev->next;
}
cout<<curr->value<<"->";
prev->next = curr->next;
delete curr;
curr = prev->next;
}
cout<<curr->value<<endl;
delete curr;
}
添加回答
举报
0/150
提交
取消