//用指针实现插入排序
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int main(){
int i,j,k,m,n,x;
struct node *h,*p,*q;
scanf("%d",&n);
scanf("%d",&x);
h=new node;
h->data=x;
h->next=NULL;
for(i=2;i<=n;i++){
scanf("%d",&x);
q=new node;
q->data=x;
q->next=NULL;
if(q->data<h->data){
q->next=h;
h=q;
continue;
}
p=h;
while(p->next!=NULL && q->data >= p->next->data)p=p->next;
if(p->next==NULL){
p->next=q;
continue;
}
q->next=p->next;
p->next=q;
}
p=h;
while(p!=NULL){
printf("%d%c",p->data,p->next==NULL?'\n',' ');
p=p->next;
}
system("pause");
return 0;
}
//约瑟夫环
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int main(){
int i,j,k,m,n;
struct node *h,*p,*q;
scanf("%d%d",&n,&m);
h=new node;
h->data=1;
h->next=h;
p=h;
for(i=2;i<=n;i++){
q=new node;
q->data=i;
q->next=p->next;
p->next=q;
p=q;
}
p=h;
k=1;
while(p->next!=p){
if(k<m-1){
k++;
p=p->next;
}else if(k==m-1){
q=p->next;
p->next=p->next->next;
printf("%d--",q->data);
free(q);
k=1;
p=p->next;
}
}
printf("大王是:%d\n",p->data);
system("pause");
return 0;
}
结构体指针实现插入排序和解决约瑟夫环问题
最新推荐文章于 2022-05-31 21:47:16 发布