發新話題

[問題] 請教哪裡出錯了...dubug有31個錯...

請教哪裡出錯了...dubug有31個錯...

#include   <malloc.h>
#include   <stdio.h>
struct   node
{
int   data;
struct   node   *next;
};

void   sort(node*   head)
{
node   *pre,*p1,*p2;
node   *tail=0;

int   nLen=0;
p1=head;
while(p1)
{
nLen++;
p1=p1-> next;
}

if(nLen==0   ||   nLen==1)return;
p1=head;p2=p1-> next;
for(int   i=1;i <nLen;++i)
{
while(p2!=tail)
{
if(p1-> data   >   p2-> data)
{
if(p1==head){
p1-> next=p2-> next;
p2-> next=p1;
pre=p2;
p1=pre-> next;p2=p1-> next;
}
else
{
pre-> next=p2;
p1-> next=p2-> next;
p2-> next=p1;
p1=pre-> next;p2=p1-> next;
}
}
pre=p1;p1=p1-> next;p2=p2-> next;
}
tail=p1;p1=head;p2=p1-> next;
}
}

void   main()
{
int   a[];

node   *head=0,*p,*pre;
for(int   i=0;i <8;++i)
{
p=(node*)malloc(sizeof(node));
p-> data=a;
p-> next=0;
if(head==0)
head=pre=p;
else  
{
pre-> next=p;
pre=p;
}
}
printf( "Before   sort: ");
p=head;
while(p)
{
printf( "%4d ",p-> data);
p=p-> next;
}
printf( "\n ");

sort(head);
printf( "Before   sort: ");
p=head;
while(p)
{
printf( "%4d ",p-> data);
p=p-> next;
}
printf( "\n ");
}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。