本篇内容介绍了“josephus怎么实现不带头节点的循环链表”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
#include <iostream> #include <cstdlib> using namespace std; typedef struct node { int data; struct node *next; }NODE; NODE* create_circlelist(int n) //建立不带头节点的循环单链表 { NODE* head; NODE* p; NODE* q; p = new NODE; p->data = 1; head = p; for(int i=2;i<=n;i++) { q = new NODE; q->data = i; p->next = q; p = q; //p永远指向最后一个节点 } p->next = head; return head; } void josephus(NODE* head,int pos,int step) { int i=1; NODE* front = NULL; NODE* temp = head; while(temp->data != pos) { temp = temp->next; } while(temp!=temp->next) { for(int i=0;i<step-1; i++) { front = temp; temp = temp->next; } cout<<"the"<<i++<<"kill number is:"; cout<<temp->data<<" "<<endl; front->next = temp->next; delete temp; temp = front->next; } cout<<"the "<<i++<<" kill Number is:"; cout<< temp->data<<endl; delete temp; } #if 1 int main(int n, char*m[]) { cout<<"input the size:"; int size; cin>>size; cout<<"input the start pos:"; int pos; cin>>pos; cout<<"input the circle number:"; int step; cin>>step; NODE* head = create_circlelist(size); josephus(head,pos,step); return 0; } #else int main(int n, char*m[]) { int size=atoi(m[1]); int pos =atoi(m[2]); int step =atoi(m[3]); NODE* head = create_circlelist(size); josephus(head,pos,step); return 0; } #endif
“josephus怎么实现不带头节点的循环链表”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。