时间节点2011-04-25
/****************************************************
* FileName: sort_list.c
* Usage: insert to the list_node
* sort the list
* **************************************************
*/
#include<stdio.h>
#include<stdlib.h>
struct List
{
int number;
struct List *next;
};
typedef struct List Node;
typedef Node *Link;
Link Create_List(Link ead);
Link insert_rank(Link head);
void Print_List(Link head);
void Free_List(Link head);
/*----------------------------------*/
/* main */
/*----------------------------------*/
int main(void)
{
Link head=NULL;
head=Create_List(head);
if(head != NULL) {
Print_List(head);
insert_rank(head); /* sort the rank */
printf("\n======= after ranking the list =======\n");
Print_List(head);
Free_List(head);
}
return 0;
}
/* Create the list */
Link Create_List(Link head)
{
Link New=NULL;
Link Pointer=NULL;
Link r=NULL;
head=(Link)malloc(sizeof(Node));
head->next=NULL;
r=head;
if(head == NULL) {
printf("memory allocate failure!\n");
}
New=(Link)malloc(sizeof(Node));
if(New == NULL){
printf("memory allocate failure!\n");
}
printf("*** Input DataNumber exit for 0 ***\n");
scanf("%d",&New->number);
while(New->number != 0){
r->next=New;
r=New;
New=(Link)malloc(sizeof(Node));
if(New == NULL){
printf("memory allocate failure!\n");
}
scanf("%d",&New->number);
New->next=NULL;
}
return head;
}
Link insert_rank(Link head)
{
int flag;
Link L=NULL;
Link point=NULL;
Link New=NULL;
Link q=NULL;
Link p=NULL;
Link s=NULL;
L=head; /* storage the head point */
for( point=L->next; point!=NULL; ) {
flag=0;
q=point->next;
p=L;
while( q!=NULL && p!=q ) {
s=p;
p=p->next;
if(q->number < p->number) {
flag=1;
New=(Link)malloc(sizeof(Node));
if(New == NULL){
printf("memory allocate failure!\n");
}
New->number=q->number;
New->next=p;
s->next=New;
/* delete the node */
point->next=q->next;
free(q);
break;
}
}
if(flag == 1)
continue;
else
point=point->next;
}
return head;
}
void Print_List(Link head)
{
Link Pointer=NULL;
Pointer=head->next;
printf("*** Output Data ***\n");
while(Pointer != NULL) {
printf("number is: %d\n",Pointer->number);
Pointer=Pointer->next;
}
}
void Free_List(Link head)
{
Link Pointer=NULL;
while(head != NULL){
Pointer=head;
head=head->next;
printf("Wait...............\n");
free(Pointer);
}
}
470

被折叠的 条评论
为什么被折叠?



