typedefstruct List
{
ElemType data;struct List *next;struct List *prior;}Dulode,*DuLinkList;
二 初始化链表
voidInitList(DuLinkList &L){
L =(DuLinkList)malloc(sizeof(Dulode));// use the L to point to the store place we malloc
L->next = L;
L->prior = L;}
三 从头部插入元素
voidpush(DuLinkList L,ElemType e){
DuLinkList p = L;
DuLinkList q =(DuLinkList)malloc(sizeof(Dulode));
q->data = e;
q->next = L->next;
q->prior = p;
L->next->prior = q;
L->next = q;//insert the elem to the head}
四 从尾部插入元素
voidpush_back(DuLinkList L,ElemType e){
DuLinkList p = L;
DuLinkList q =(DuLinkList)malloc(sizeof(Dulode));
q->data = e;
q->next = p;
q->prior = L->prior;
L->prior->next = q;
L->prior = q;}//insert to the tail of the LinkList
五 删除元素
1根据指针删除
voidDelete(DuLinkList q){
q->next->prior = q->prior;
q->prior->next = q->next;free(q);}// free the space that pointed by the q, and Link its next and prior
2 根据值删除
status Delete_by_data(DuLinkList L,ElemType data){
DuLinkList p = L;
DuLinkList q = L;while(q->next!=p)// because we use the two pointers, so we can judge whether we have look the all elements{if(q->next->data == data){free(q);break;}
q = q->next;}if(q->next == p)return0;elsereturn1;}
3升序排序
voidsort_up(DuLinkList L){
DuLinkList p,q;
DuLinkList NewList =(DuLinkList)malloc(sizeof(Dulode));// we use a NewList to store the right order of elements
NewList->next = NewList;
NewList->prior = NewList;//now we have initilize it
p = L;
q = L;while(p->next!=q){// while we haven't traverse the list for a whole round,keep point to the next
DuLinkList temp =(DuLinkList)malloc(sizeof(Dulode));
temp->data = p->next->data;// malloc a new node with the same dataif(NewList->next==NewList&&NewList->prior==NewList){// judge whether it is empty
NewList->next = temp;
NewList->prior = temp;
temp->next = NewList;
temp->prior = NewList;}else{// now we divide it into three situations// first the data is small than the head nodeif(temp->data<NewList->next->data){
NewList->next->prior = temp;
temp->next = NewList->next;
temp->prior = NewList;
NewList->next = temp;}// second the data is bigger than the tail nodeelseif(temp->data>NewList->prior->data){
NewList->prior->next = temp;
temp->prior = NewList->prior;
temp->next = NewList;
NewList->prior = temp;}// third the data is in the middleelse{
DuLinkList p1 = NewList;while(p1->next->data<temp->data){
p1 = p1->next;}
temp->next = p1->next;
temp->prior = p1;
p1->next =temp;}}
p = p->next;}
p = L->next;
q = L;// now we free the storage placewhile(q!=p){
p = p->next;free(p->prior);}
L->next = NewList->next;
L->prior = NewList->prior;
NewList->next->prior = L;
NewList->prior->next = L;}