自己写的链表倒置和链表排列程序

链表倒置:

/*************************************************************************
* author:qiaoliang328
* date:2010-02-08
* function:测试链表倒序
*************************************************************************/
#include <stdio.h>
#include <assert.h>
typedef struct student
{
int num;
struct student *next;
}student_t;

student_t *convert_list(student_t *head);
int main(void)
{
student_t *new,*this,*head;
student_t *result;
student_t temp;
this=head=&temp;
int i;
for(i=0;i<20;i++)
{
new=(student_t *)malloc(sizeof(student_t));
new->num=i+10;
new->next=NULL;
this->next=new;
this=new;

}
this=head;
head=this->next;
this=head;
for(i=0;i<20;i++)
{
printf("%d ",this->num);
this=this->next;
}
printf("/nbefore convert/n");
printf("/nafter convert/n");
result=convert_list(head);
this=result;
for(i=0;i<20;i++)
{
printf("%d ",this->num);
this=this->next;
}
return 0;
}

student_t *convert_list(student_t *head)
{
student_t *old_head=head,*old_this;
student_t *new_head,*new_this,*old_last;
student_t temp;
int node_count=0,i;
new_head=new_this=&temp;
old_this=old_head;
node_count++;
while(old_this->next!=NULL)
{
old_this=old_this->next;
node_count++;
}
//printf("qljt-----------convert 1/n");
for(i=0;i<node_count;i++)
{

//printf("qljt-----------convert 2/n");
old_this=old_head;
while(old_this->next!=NULL)
{
old_last=old_this;
old_this=old_this->next;
}
new_this->next=old_this;
new_this=old_this;
old_last->next=NULL;
}
new_this=new_head;
new_head=new_this->next;
return (new_head);
}

///////////////////////////////////////////////////////////////////////

链表排序:

/*************************************************************************
* author:qiaoliang328
* date:2010-02-08
* function:测试链表的排序
*************************************************************************/

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
typedef struct student
{
int num;
struct student *next;
}student_t;

student_t *taxis_list(student_t *head);
int main(void)
{
student_t *new,*head;
student_t *result;
student_t temp;
this=head=&temp;
int i;
srand((int)time(0));
for(i=0;i<20;i++)
{
new=(student_t *)malloc(sizeof(student_t));
new->num=rand()%100;
new->next=NULL;
this->next=new;
this=new;

}
this=head;
head=this->next;
this=head;
for(i=0;i<20;i++)
{
printf("%d ",this->num);
this=this->next;
}
printf("/nbefore convert/n");
printf("/nafter convert/n");
result=taxis_list(head);

//printf("qljt-----------main 1/n");
this=result;

//printf("qljt-----------main 2/n");
for(i=0;i<20;i++)
{
//printf("qljt-----------main i=%d/n",i);
printf("%d ",this->num);
this=this->next;
}
return 0;
}

student_t *taxis_list(student_t *head)
{
student_t *old_head=head,*old_this,*old_last,*old_min,*old_min_last;
student_t *new_head,*new_this;
student_t temp;
int node_count=0,i;
new_head=new_this=&temp;
old_this=old_head;
node_count++;
while(old_this->next!=NULL)
{
old_this=old_this->next;
node_count++;
}
//printf("qljt-----------convert 1/n");
for(i=0;i<node_count;i++)
{

//printf("qljt-----------convert 2/n");
old_this=old_head;
old_min=old_head;
old_min_last=old_head;
while(old_this->next!=NULL)
{
//printf("qljt-----------convert 3/n");
old_last=old_this;
old_this=old_this->next;
if(old_min->num > old_this->num)
{
old_min=old_this;
old_min_last=old_last;
}
}
if(old_min!=old_head)
{
new_this->next=old_min;
new_this=old_min;
old_min_last->next=old_min->next;
new_this->next=NULL;
}
else
{
new_this->next=old_head;
new_this=old_head;
old_head=old_head->next;
new_this->next=NULL;

}}new_this=new_head;new_head=new_this->next;printf("qljt-----------convert 4/n");return (new_head);}

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...