约瑟夫环的链表C++实现

来源:互联网 发布:cad注册表清理软件 编辑:程序博客网 时间:2024/06/04 08:58
#include<iostream>using namespace std;typedef struct Node{int data;Node*next;} NodeList;NodeList* CreateList(int n){NodeList*p,*q,*head;int i=1;p= new NodeList();p->data=i;head=p;for( i=1;i<n;i++){q=new NodeList();q->data=i+1;p->next=q;p=q;}p->next=head;return head;delete p;p=NULL;delete q;q=NULL;}void deletem(NodeList*L,int m){NodeList*p,*q,*temp;int j;p=L;while (p->next!=p){for (int j=1;j<m;j++){q=p;p=p->next;}cout<<p->data<<endl;temp=p;q->next=p->next;p=p->next;delete temp;}cout<<p->data<<endl;}int main(){int n=10,m=2;NodeList*head1;head1=CreateList(n);deletem(head1,m);}

0 0
原创粉丝点击