08单链表的正向排序
来源:互联网 发布:c语言floor函数 编辑:程序博客网 时间:2024/06/05 03:14
运行的结果如下所示:
代码如下所示:
#include <stdio.h>
#include <malloc.h>
#include <malloc.h>
/*单链表的正向排序*/
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct node
{
int data;
struct node *next;
}node;
node *InsertSort(void)
{
int data = 0;
struct node *head = NULL , *New , *Cur , *Pre;
while(1)
{
printf("please input the data:\n");
scanf("%d" , &data);
if(data == 0)
{
break; /*输入0结束*/
}
New = (struct node*)malloc(sizeof(struct node));
New->data = data; /*新分配一个node节点*/
New->next = NULL;
if(head == NULL)
{ /*第一次循环时对头节点赋值*/
head = New;
continue;
}
if(New->data <= head->data)
{
/*head之前插入节点*/
New->next = head;
head = New;
continue;
}
{
int data = 0;
struct node *head = NULL , *New , *Cur , *Pre;
while(1)
{
printf("please input the data:\n");
scanf("%d" , &data);
if(data == 0)
{
break; /*输入0结束*/
}
New = (struct node*)malloc(sizeof(struct node));
New->data = data; /*新分配一个node节点*/
New->next = NULL;
if(head == NULL)
{ /*第一次循环时对头节点赋值*/
head = New;
continue;
}
if(New->data <= head->data)
{
/*head之前插入节点*/
New->next = head;
head = New;
continue;
}
Cur = head;
while(New->data > Cur->data && Cur->next != NULL) /*找到需要插入的位置*/
{
Pre = Cur;
Cur = Cur->next;
}
if(Cur->data >= New->data) /*位置在中间*/
{
Pre->next = New;
New->next = Cur; /*new节点插入到Pre和Cur之间*/
}
else /*位置在末尾*/
{
Cur->next = New; /*把new节点插入到cur之后*/
}
}
return head;
}
while(New->data > Cur->data && Cur->next != NULL) /*找到需要插入的位置*/
{
Pre = Cur;
Cur = Cur->next;
}
if(Cur->data >= New->data) /*位置在中间*/
{
Pre->next = New;
New->next = Cur; /*new节点插入到Pre和Cur之间*/
}
else /*位置在末尾*/
{
Cur->next = New; /*把new节点插入到cur之后*/
}
}
return head;
}
void print(node *head)
{
node *p;
if(head == NULL)
{
printf("The Link is empty!\n");
return;
}
{
node *p;
if(head == NULL)
{
printf("The Link is empty!\n");
return;
}
p = head;
while(p != NULL)
{
printf("%d " , p->data);
p = p->next;
}
}
while(p != NULL)
{
printf("%d " , p->data);
p = p->next;
}
}
void main()
{
node *head = InsertSort();
print(head);
printf("\n***********************************\n");
}
{
node *head = InsertSort();
print(head);
printf("\n***********************************\n");
}
- 08单链表的正向排序
- 单链表的正向排序,反向排序
- 正向插入排序
- 单链表正向排序、逆置、寻找中间元素、打印单链表操作
- 正向代理的概念
- hibernate的正向使用
- 面试题五 C/C++面试秘笈 之链表的正向排序--程序员面试题
- 单链表逆置、正向排序、打印、寻找中间元素
- List集合正向和逆向排序
- 正向索引和倒排序索引
- 浅谈lucene的正向文档
- nginx正向代理的不足
- Nginx正向代理的例子
- Squid正向代理的身份验证
- 正向反向代理的理解
- JS表格排序(支持正向,逆向,以及类型判断)
- 一段正向及横向的统计图表
- 正向生活渗透的手机二维码
- 科研洞察力(转载)
- 深入浅出分区表与分区索引之三:分区重要概念和术语
- XCode RUN时出现The identity used to sign the executable is no longer valid
- 深入浅出分区表与分区索引之四:创建分区表的步骤
- 导入excel2007时网页报null
- 08单链表的正向排序
- I2C串行总线的组成及工作原理
- 深入浅出分区表与分区索引之四:案例讲解(上)
- 程序员要勇于说不
- 终于认识到【精力有限,少加分散】
- 多线程,日期calendar
- Eclipse中自动生成author等注释
- 09双向链表的操作
- Linux如何理解用数字表示文件的操作权限