|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
typedef struct LIST{
int num;
LIST* next;
LIST* front;
int longth;
}List;
void creat(List* head,int n){
List* head1 = head;
system("pause");
int i = 0,m=0;
for(i=1;i<n;i++){
List* node = new List;
node->next = NULL;
node->num = i+1;
head1->next = node;
node->front = head1;
head1 = head1->next;
// cout<<"i = "<<i<<" head = "<<head1->num<<endl;
}
head1->next = head;
head->front = head1;//定义head的front应该在最后
}
void bian(List* head,int n){
List* head1;
head1 = head;
int i=0;
for(i=0;i<n+2;i++){
cout<<head1->num<<endl;
head1 = head1->next;
}
}
void Delete(List* head1,int m,int n){
int i=0;
while(head1->longth!=1){
if(i == m-1&&m!=1){// 中间
delete head1->next;
head1->next = head1->next->next;
head1->longth--;
bian(head1,head1->longth);
}
else if(i==m-1&&m==1){// 开头
List* h = new List;//is used for replaceing
h = head1;
h->front = h->next;
h = h->next;
delete h;
head1->longth--;
bian(head1,head1->longth);
}
else{
i++;
head1 = head1->next;
}
//List* head1 = new List;
//head1 = head2;
/*if(i==m-1){
head1->front = head1->next;
delete head1;
head->longth--;
cout<<head->longth<<endl;
i = 0;
}
else{
head2 = head2->next;
system("pause");
i++;
}*/
i = i%n;
}
}
int main(){
int i=0,n=0,m=0;
cin>>n>>m;
List* head = new List;
List* head1 = new List;
head->num = 1;
head->longth = n;
// cout<<head->longth<<endl;
head->next = NULL;
head->front = NULL;
head1 = head;
creat(head,n);
Delete(head1,m,n);
cout<<head->num<<endl;
return 0;
} |
|