对链表进行排序,根据含字母和数字的字符串,但实现不了
结构如下:
typedef struct
{
char pool_name[4];
struct *next;
}POOL_BASIC;
其中pool_name就是A01, A03, A02, B02,这样的,按字母和数字的顺序进行排序,即排成A01,A02, A03,B02,这样
我的程序如下:效果是 没有效果,拙急~~~~~。。。
//按塘号排序
POOL_BASIC* order_pool_name(POOL_BASIC *head)
{
POOL_BASIC* kz; //控制循环比较的指针
POOL_BASIC* p; //临时指针变量
POOL_BASIC* p1;
POOL_BASIC* p2;
if(head==NULL)
{
printf("没有信息,无法排序!");
getchar();
return NULL;
}
if(head->next==NULL)
{
printf("只有一个结点,无须排序");
getchar();
return head;
}
p1=(POOL_BASIC*)malloc(sizeof(POOL_BASIC*));
p1->next=head; //增加一个节点,放在第一个节点的前面,作为前驱结点
head=p1;
for(kz=NULL;kz!=head;kz=p)
{
for(p=p1=head;p1->next->next!=kz;p1=p1->next)
{
if(p1->next->next->pool_name[0]<p1->next->pool_name[0])
{
p2 = p1->next->next;
p1->next->next = p2->next;
p2->next = p1->next;
p1->next = p2;
p = p1->next->next;
}
else if(p1->next->next->pool_name[0]==p1->next->pool_name[0])
{
if(atoi(p1->next->next->pool_name)<atoi(p1->next->pool_name))
{
p2 = p1->next->next;
p1->next->next = p2->next;
p2->next = p1->next;
p1->next = p2;
p = p1->next->next;
}
}
}
}
p1=head;
head=head->next;
free(p1);
p1=NULL;
printf("已经按塘号字母数字顺序 排序完毕!");
getchar();
return head;
}
[解决办法]
传说中的C++吧!看没