链表倒置:
/*************************************************************************
* 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);}