圆圈中最后剩下的数字

#include using namespace std; typedef struct LinkNode { int data_in; LinkNode *next; }; LinkNode* LasterNum(LinkNode *node,int m); int main() { LinkNode *node,*head; node=new LinkNode; head=node; int data_in; cin>>data_in; node->data_in=data_in; while(1) { cin>>data_in; if (data_in==0) { break;; } LinkNode *temp; temp=new LinkNode; temp->data_in=data_in; node->next=temp; node=temp; } node->next=NULL; int m; cin>>m; LinkNode *LasterNumber=LasterNum(head,m); system(“pause”); return 0; } LinkNode* LasterNum(LinkNode *node,int m) { if (node==NULL) { return NULL; } //组成循环链表 LinkNode *head; head=node; while(1) { if (node->next==NULL) { break; } node=node->next; } node->next=head; node=head; //循环访问链表,删除第每个 int i=1; while(head->next) { //如果进入m-1各节点,把m删了 if (i==(m-1)) { head=node->next->next; delete node->next; node->next=head; node=head; i=1; if (head->next==head) { break; } } else { node=node->next; i++; } } head->next=NULL; return head; }

点赞