数据结构之单链表的冒泡排序

 
  
#include
#include
typedef struct Node
{
int data;
struct Node *next;
}List;
List *Creat()
{
int i,n;
scanf("%d",&n);
List *head,*p1,*p2;
p1=p2=(List *)malloc(sizeof(List));
head=NULL;
for(i=0;i
{
if(head==NULL) head=p1;
else p2->next=p1;
scanf("%d",&p1->data);
p2=p1;
p1=(List *)malloc(sizeof(List));
p2->next=p1;
}
p2->next=NULL;
return head;
}
List *Sort(List *head)
{
int data;
List *p1,*p2;
for(p1=head;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if(p1->datadata)
{
data=p2->data;
p2->data=p1->data;
p1->data=data;
}
}
}
return head;
}
void print(List *head)
{
List *p;
p=head;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
List *head;
List *head1;
head=Creat();
head1=Sort(head);
print(head);
}

你可能感兴趣的:(第二章:线性表)