typedef struct node
{ ElemType data;
struct node *prior*next;
}node*DLinkedList;
void TwoWayBubbleSort(DLinkedList la)
//對存儲在帶頭結點的雙向鏈表la中的元素進行雙向起泡排序
{int exchange=; // 設標記
DLinkedList ptemptail;
head=la //雙向鏈表頭算法過程中是向下起泡的開始結點
tail=null; //雙向鏈表尾算法過程中是向上起泡的開始結點
while (exchange)
{p=head>next; //p是工作指針指向當前結點
exchange=; //假定本趟無交換
while (p>next!=tail) // 向下(右)起泡一趟有一最大元素沉底
if (p>data>p>next>data) //交換兩結點指針涉及條鏈
{temp=p>next; exchange=;//有交換
p>next=temp>next;temp>next>prior=p //先將結點從鏈表上摘下
temp>next=p; p>prior>next=temp; //將temp插到p結點前
temp>prior=p>prior; p>prior=temp;
}
else p=p>next; //無交換指針後移
tail=p; //准備向上起泡
p=tail>prior;
while (exchange && p>prior!=head) //向上(左)起泡一趟有一最小元素冒出
if (p>data<p>prior>data) //交換兩結點指針涉及條鏈
{temp=p>prior; exchange=; //有交換
p>prior=temp>prior;temp>prior>next=p //先將temp結點從鏈表上摘下
temp>prior=p; p>next>prior=temp; //將temp插到p結點後(右)
temp>next=p>next; p>next=temp;
}
else p=p>prior; //無交換指針前移
head=p; //准備向下起泡
}// while (exchange)
} //算法結束
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23173.html