对链表进行排序
2012-08-10 00:00
281 查看
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *pnext;
};
struct node *creat_list();//创建链表函数
void traverse_list(struct node *phead);//遍历链表函数
bool is_empty(struct node *phead);//判断链表是否为空函数
int lenght_list(struct node *phead);//计算链表长度的函数
void sort_list(struct node *phead);//对链表进行排序
int insert_list(struct node *phead,int weizhi,int shu );//插入节点函数
int delete_list(struct node *phead ,int weihzi);//删除节点函数
int main()
{
struct node *phead;
phead=creat_list();
traverse_list(phead);
if(is_empty(phead))
printf("链表不为空!\n");
else
printf("链表为空!\n");
int len=lenght_list(phead);
printf("链表的节点个数为:%d\n",len);
printf("排序后的链表为:");
sort_list(phead);
traverse_list(phead);
insert_list(phead, 3,6666);
traverse_list(phead);
delete_list(phead,3);
traverse_list(phead);
return 0;
}
struct node *creat_list()
{
struct node *phead,*ptail,*pnew;
phead=(struct node *)malloc(sizeof(struct node));
ptail=phead;
ptail->pnext=NULL;
if(NULL==phead)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
int len;//表示要创建的节点数
int val;
printf("请输入要创建的节点数:");
scanf("%d",&len);
for(int i=0;i<len;i++)
{
printf("请输入%d个节点的数据:",i+1);
scanf("%d",&val);
pnew=(struct node *)malloc(sizeof(struct node));
if(NULL==pnew)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
pnew->data=val;
ptail->pnext=pnew;
pnew->pnext=NULL;
ptail=pnew;
}
return phead;
}
void traverse_list(struct node *phead)
{
struct node *p=phead->pnext;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pnext;
}
printf("\n");
return ;
}
bool is_empty(struct node *phead)
{
if(phead->pnext!=NULL)
return true;
else
return false;
}
int lenght_list(struct node *phead)
{
struct node *p=phead->pnext;
int len=0;
while(p!=NULL)
{
len++;
p=p->pnext;
}
return len;
}
void sort_list(struct node *phead)
{
int len=lenght_list(phead);//链表的长度
int i,j;
struct node *p,*q;
if(len==0)
printf("链表为空,无需排序!\n");
for(i=0,p=phead->pnext;i<len-1;i++,p=p->pnext)
{
for(j=i+1,q=p->pnext;j<len;j++,q=q->pnext)
{
if(p->data>q->data)
{
int temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
return ;
}
int insert_list(struct node *phead,int weizhi,int shu )
{
int i=0;
struct node *p=phead;
while(NULL!=p && i<weizhi-1)//很好的算法
{
p=p->pnext;
i++;
}
if(i>weizhi-1 || NULL==p)
return false;
struct node *pnew=(struct node *)malloc(sizeof(struct node));
if(NULL==pnew)
{
printf("内存分配失败!\n");
}
pnew->data=shu;
struct node *ptail=p->pnext;
p->pnext=pnew;
pnew->pnext=ptail;
//free(ptail); //为什么不可以释放掉呢
return true;
}
int delete_list(struct node *phead ,int weizhi)
{
int i=0;
struct node *p=phead;
while(NULL!=p->pnext && i<weizhi-1)//很好的算法
{
p=p->pnext;
i++;
}
if(i>weizhi-1 || NULL==p)
return false;
struct node *ptail;//临时指针
ptail=p->pnext;
p->pnext=ptail->pnext;
free(ptail);
return true;
}
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *pnext;
};
struct node *creat_list();//创建链表函数
void traverse_list(struct node *phead);//遍历链表函数
bool is_empty(struct node *phead);//判断链表是否为空函数
int lenght_list(struct node *phead);//计算链表长度的函数
void sort_list(struct node *phead);//对链表进行排序
int insert_list(struct node *phead,int weizhi,int shu );//插入节点函数
int delete_list(struct node *phead ,int weihzi);//删除节点函数
int main()
{
struct node *phead;
phead=creat_list();
traverse_list(phead);
if(is_empty(phead))
printf("链表不为空!\n");
else
printf("链表为空!\n");
int len=lenght_list(phead);
printf("链表的节点个数为:%d\n",len);
printf("排序后的链表为:");
sort_list(phead);
traverse_list(phead);
insert_list(phead, 3,6666);
traverse_list(phead);
delete_list(phead,3);
traverse_list(phead);
return 0;
}
struct node *creat_list()
{
struct node *phead,*ptail,*pnew;
phead=(struct node *)malloc(sizeof(struct node));
ptail=phead;
ptail->pnext=NULL;
if(NULL==phead)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
int len;//表示要创建的节点数
int val;
printf("请输入要创建的节点数:");
scanf("%d",&len);
for(int i=0;i<len;i++)
{
printf("请输入%d个节点的数据:",i+1);
scanf("%d",&val);
pnew=(struct node *)malloc(sizeof(struct node));
if(NULL==pnew)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
pnew->data=val;
ptail->pnext=pnew;
pnew->pnext=NULL;
ptail=pnew;
}
return phead;
}
void traverse_list(struct node *phead)
{
struct node *p=phead->pnext;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pnext;
}
printf("\n");
return ;
}
bool is_empty(struct node *phead)
{
if(phead->pnext!=NULL)
return true;
else
return false;
}
int lenght_list(struct node *phead)
{
struct node *p=phead->pnext;
int len=0;
while(p!=NULL)
{
len++;
p=p->pnext;
}
return len;
}
void sort_list(struct node *phead)
{
int len=lenght_list(phead);//链表的长度
int i,j;
struct node *p,*q;
if(len==0)
printf("链表为空,无需排序!\n");
for(i=0,p=phead->pnext;i<len-1;i++,p=p->pnext)
{
for(j=i+1,q=p->pnext;j<len;j++,q=q->pnext)
{
if(p->data>q->data)
{
int temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
return ;
}
int insert_list(struct node *phead,int weizhi,int shu )
{
int i=0;
struct node *p=phead;
while(NULL!=p && i<weizhi-1)//很好的算法
{
p=p->pnext;
i++;
}
if(i>weizhi-1 || NULL==p)
return false;
struct node *pnew=(struct node *)malloc(sizeof(struct node));
if(NULL==pnew)
{
printf("内存分配失败!\n");
}
pnew->data=shu;
struct node *ptail=p->pnext;
p->pnext=pnew;
pnew->pnext=ptail;
//free(ptail); //为什么不可以释放掉呢
return true;
}
int delete_list(struct node *phead ,int weizhi)
{
int i=0;
struct node *p=phead;
while(NULL!=p->pnext && i<weizhi-1)//很好的算法
{
p=p->pnext;
i++;
}
if(i>weizhi-1 || NULL==p)
return false;
struct node *ptail;//临时指针
ptail=p->pnext;
p->pnext=ptail->pnext;
free(ptail);
return true;
}
相关文章推荐
- 对链表中的数字进行排序《梁勇2011java语言程序设计基础篇》22章java集合框架编程练习题22.6
- 单链表进行排序-通过节点交换,不通过值交换
- 面试中常见链表问题9:对单链表进行插入排序
- 单向链表:在插入值的同时要进行排序
- 采用选择排序法对链表进行排序,注意交换操作中不是对链表里某一节点里的某一元素进行交换,而是对两节点指针的交换
- 对链表进行归并排序
- 用直接插入排序法对单链表进行排序
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- C语言单向动态链表程序,实现链表的建立,合并,重新排序,链表元素的插入与删除,以及根据元素成员的值进行元素删除。
- 对链表进行排序
- 采用单链表进行冒泡排序
- C++实现两个已经排序的链表进行合并
- 用归并排序对链表进行排序
- 对链表进行排序(归并排序)
- C++实现两个已经排序的链表进行合并
- 数据结构List实例(四):使用归并排序对单链表进行排序
- 对链表进行排序 搜狗2012笔试
- 对一个存储学生信息的单向链表,按照学号升序对链表进行排序,每个节点包含了一个学生ID
- 对链表进行排序(不能额外空间,时间为O(NL))
- SDUT_2121数据结构实验之链表六:有序链表的建立 (对建立的单项链表结构进行排序)