假设有一带头结点的链表,表头指针为HEAD,每个结点含三个域:DATA,NEXT和PRIOR。其中DATA为整型,NEXT和PRIOR均为指针域。现在所有点已经由NEXT域连接起来,试编写一算法,利用PRIOR域(初值为NULL)把所有结点按其DATA里的值从小到大(升序)的顺序链接起来。
typedef struct
{ int data;
struct *next;
struct *prior;
}LINK;
#define null 0
void link_asc (LINK *head)
{ if (head==null || head->next==null) printf("the link needn't cending!\n");
else{ LINK *Psave=head,*p1,*nowP; int f,x;
for (p1=head->next,f=0;;)
{ while (p1->next!=null)
{if (p1->prior==null) { x=p1->data;f=1;break;}
else p1=p1->next;}
if(f==0 && p1->prior==null ) {Psave->prior=p1;return;}
if(f==0) {Psave=null;return;}
p1=head->next;
while (p1->next!=null)
{ if (p1->prior==null) if(x>=p1->data) {x=p1->data;nowP=p1;}
p1=p1->next;}
if(p1->prior==null) if(x>=p1->data){x=p1->data;nowP=p1;}
Psave->prior=nowP;Psave=nowP;Psave->prior=Psave->next;}}}