设计一个算法,将一个带头结点的非空循环单链表L中最后一个最小值结点移动到表头。
请问为什么运行不起来?
#include<stdio.h>
#include<malloc.h>
typedef struct node{
int data;
struct node* next;
}node;
node *creat(int length){ // 创建循环单链表
node p;
printf("Input %d data:",length);
int n;
if(length<1)
return NULL;
node* head=(node*)malloc(sizeof(node));
head->next=NULL;
int i=1;
node temp=head;
while(i<=length){
p=(node*)malloc(sizeof(node));
scanf("%d",&n);
p->data=n;
temp->next=p;
temp=p;
i++;
}
temp->next=head->next; // 唯一和单链表创建有区别的地方
return head;
}
void print(nodeL,int n){ // 输出链表元素
nodep=L->next;
if(p==NULL){
printf("链表元素为空!");
return ;
}
int i;
for(i=0;i<n;i++){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Move(node*L,int n)
{
node *p=L->next,*pre=L;
node *minp=p,*minpre=L;
while(p!=L)
{
if (p->data<=minp->data)
{
minp=p;
minpre=pre;
}
pre=p;//pre、p同步后移
p=p->next;
}
minpre->next=minp->next;//删除minp结点
minp->next=L->next;//将minp结点插入到头结点之后
L->next=minp;
}
int main(){
node* L;
int m,n;
printf("Input n:");
scanf("%d",&n);
L=creat(n);
m=Move(L);
print(L,n);
print(L,n+1);
}