?? sort.c
字號:
/***************************************
Filename:linksort
Programmer:Zheng Bolong(ZBL)
Teacher:Wu Tao(WT)
Created:09.3.24
****************************************/
#include<stdio.h>
struct number
{
int data; /*建立結構體*/
struct number *next;
};
/*struct number *sort(struct number *head)
{
struct number *temp,*p;
for(p=head;p->next->next!=NULL;p=p->next)
{
for(;)
{
temp=p->next->next;
p->next->next=p;
p->next=temp;
}
}
return(head);
}
交換指針的方法不會實現*/
struct number *sort(struct number *head)
{
struct number *p1,*p;
int temp;
for(p=head;p!=NULL;p=p->next)
{
for(p1=p->next;p1!=NULL;p1=p1->next)
{
if((p->data)>(p1->data)) /*排序函數*/
{
temp = p1->data;
p1->data = p->data;
p->data = temp;
}
}
}
return(head);
}
struct number *creat(void)
{
struct number *head;
struct number *p1,*p2;
int n = 0;
p1=p2=(struct number *)malloc(sizeof(struct number));
scanf("%d",&p1->data);
head=NULL;
while(p1->data!=0)
{
n=n+1; /*建立鏈表*/
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct number *)malloc(sizeof(struct number));
scanf("%d",&p1->data);
}
p2->next=NULL;
return(head);
}
void main()
{
int n;
struct number *head;
struct number *p;
printf("Enter the number of nodes to sort:");
scanf("%d",&n);
printf("The number of nodes to sort is:%d\n",n);
head=creat();
head=sort(head); /*主函數*/
p=head;
while(p->data!=0)
{
printf("%d\n",p->data);
p=p->next;
}
getch();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -