插入排序(单链表)
来源:互联网 发布:加密软件破译 编辑:程序博客网 时间:2024/06/18 16:47
#include<stdio.h>
#include<stdlib.h>
struct arry{
long num;
struct arry *next;
};
main()
{
char end_input,sort_type;
long num,crls=0;/*crls用于判断输出是否需要换行*/
unsigned long p_count=0;/*指针编号计数器*/
struct arry *head,*cur,*temp,*last;/*t1t2用于输入,f1f2用于查找和排序*/
head=(struct arry*)malloc(sizeof(struct arry));
head->next=NULL;
last=cur=head;
puts("Please choose a type of sort:U(High-->Low)/D(Low-->High)");
printf(">>");/*仿Unix输入提示符^_^*/
scanf("%c",&sort_type);
getchar();/*吸收多余的回车*/
while (sort_type!='u'&&sort_type!='U'&&sort_type!='d'&&sort_type!='D')
{
puts("Illigal input./nPlease choose a type of sort:U(High-->Low)/D(Low-->High)");
printf(">>");
scanf("%c",&sort_type);
getchar();/*吸收多余的回车*/
}/*排序类型标识符,U/u降序,D/d升序*/
do
{
temp=(struct arry*)malloc(sizeof(struct arry));
temp->next=NULL;
cur->next=temp;
cur=temp;
printf(">>");
scanf("%ld",&cur->num);
getchar();
switch (sort_type)
{
case 'u':
case 'U':
for (temp=head;temp->next!=NULL;temp=temp->next)
{
if (cur->num>temp->next->num)/*当前数字比表中的大的时候*/
{
cur->next=temp->next;
temp->next=cur;
last->next=NULL;
cur=last;/*原来的代码有一段是cur->next->next=NULL
其实这样做,会把后面的链表给打断,最大的错误*/
break;
}
if (temp->next->next==NULL)last=cur;
}
break;
case 'd':
case 'D':
for (temp=head;temp->next!=NULL;temp=temp->next)
{
if (cur->num<temp->next->num)
{
cur->next=temp->next;
temp->next=cur;
last->next=NULL;
cur=last;
/*原来的错误同上*/
break;
}
if (temp->next->next==NULL)last=cur;/*
之前代码说明cur已经接在链尾,这里是更新尾指针*/
}
break;
}/*switch语句结束*/
printf("End input?(Y for exit,other for continue)");
end_input=getchar();
getchar();
}while (end_input!='y'&&end_input!='Y');/*end_input为结束输入标识符*/
#if 0
temp=(struct arry*)malloc(sizeof(struct arry));
cur->next=temp;/*建立前件与新指针的连接*/
temp->next=NULL;
cur=temp;/*创立并且指向当前新创立的指针*/
#endif
printf("The sorted numbers:");
for (temp=head;temp->next!=NULL;temp=temp->next)
{
if (crls%5==0)putchar('/n');
printf("%10ld",temp->next->num);
crls++;
}
putchar('/n');
/*接下来释放开辟的地址*/
cur=head;
for (temp=head;cur->next!=NULL;temp=cur)
{
printf("Freed address:%X/n",temp);
cur=temp->next;
free(temp);
}
free(cur);
printf("Freed address:%X/n/n",temp);
puts("All allocated memories have been freed.");
system("pause");
}
- 排序之单链表插入排序
- 单链表插入排序
- 单链表实现插入排序
- 插入排序(单链表)
- 单链表插入排序
- 单链表插入排序
- 实现单链表插入排序
- 单链表的插入排序
- 单链表直接插入排序
- 单链表插入排序算法
- [leetcode]单链表插入排序
- 单链表的插入排序
- 单链表实现插入排序
- 单链表---直接插入排序
- 单链表插入排序
- 单链表实现插入排序
- 单链表插入删除排序
- 单链表的插入排序
- 三星将在印度推出基于Android的Galaxy i899
- 安装Ubuntu 9.10(hd0)
- 当前各大企业云计算的发展形式之其他
- C语言中输出函数任意参数的实现
- hello world
- 插入排序(单链表)
- 十进制转二进制的一种实现
- android实现结束进程。
- Oracle Profile 使用详解
- 纯C写的windows下贪吃蛇,BUG检查中。。。(2010.6.7更新)
- 分页和非分页内存
- C语言随感进行时(不定时更新)
- tag 搜索
- AutoCompleteBox学习