#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int Elemtype; typedef int Status; typedef struct Node{ Elemtype data; struct Node* prior; struct Node* next; }Node; typedef struct Node* DolLinkList; Status InitDolLinkList(DolLinkList *DL){ *DL = (DolLinkList)malloc(sizeof(struct Node)); if(!(*DL)) return ERROR; (*DL)->next = *DL; (*DL)->prior = *DL; return OK; } Status ClearDolLinkList(DolLinkList DL){ //将双向循环链表清为空表 DolLinkList s,f = DL->next; while(f != DL){ s = f; f = f->next; free(s); } DL->next = DL; DL->prior = DL; return OK; } Status DestoryDolLinkList(DolLinkList DL){ ClearDolLinkList(DL); free(DL); DL = NULL; } int Length_DolLinkList(DolLinkList DL){ int length = 0; DolLinkList t = DL->next; while(t != DL){ t = t->next; length++; } return length; } Status GetElement_DolLinkList(DolLinkList DL,int position,int *value){ int count = 0; int length = Length_DolLinkList(DL); if(position < 1 || position > length){ return ERROR; } else{ while(count < position){ DL = DL->next; count++; } *value = DL->data; return OK; } } Status LocateElement_DolLinkList(DolLinkList DL,Elemtype value,int *position){ *position = 1; DolLinkList t = DL->next; while(t != DL){ if(t->data == value){ return OK; } t = t->next; (*position)++; } return ERROR; } Status GetPriorElement_DolLinkList(DolLinkList DL,int currentElement,int *priorElement){ int r1; int count = 0; if(LocateElement_DolLinkList(DL,currentElement,&r1)){ while(count < r1){ count++; DL = DL->next; } if(count == 1) *priorElement = DL->prior->prior->data; else *priorElement = DL->prior->data; return OK; } else return ERROR; } Status GetNextElement_DolLinkList(DolLinkList DL,int currentElement,int *nextElement){ int r1; if(LocateElement_DolLinkList(DL,currentElement,&r1)){ int count = 0; while(count < r1){ count++; DL = DL->next; } if(count == Length_DolLinkList(DL)) *nextElement = DL->next->next->data; else *nextElement = DL->next->data; return OK; } else return ERROR; } Status Insert_DolLinkList(DolLinkList DL,int position,int value){ if(position < 1 || position > Length_DolLinkList(DL) + 1) return ERROR; else{ int count = 0; while(count < position){ count++; DL = DL->next; } DolLinkList new = (DolLinkList)malloc(sizeof(struct Node)); new->data = value; new->prior = DL->prior; DL->prior->next = new; new->next = DL; DL->prior = new; return OK; } } Status Delete_DolLinkList(DolLinkList DL,int position,int *value){ if(position < 1 || position > Length_DolLinkList(DL)) return ERROR; else{ int count = 0; while(count < position){ count++; DL = DL->next; } *value = DL->data; DL->next->prior = DL->prior; DL->prior->next = DL->next; free(DL); } } Status CreateDolLinkList_TailInsert(DolLinkList DL,int number){ DolLinkList new; int i; printf("PLEASE ENTER %d ELEMNET!\n",number); for(i = 1; i <= number; i++){ new = (DolLinkList)malloc(sizeof(struct Node)); if(!new) return ERROR; printf("please enter element%d: ",i); scanf("%d",&(new->data)); new->prior = DL->prior; new->next = DL; DL->prior->next = new; DL->prior = new; } return OK; } void DisplayDolLinkList(DolLinkList DL){ DolLinkList temp = DL; while(temp->next != DL){ temp = temp->next; printf("%d ",temp->data); } printf("\nDisplay Executed!\n\n"); } int main(){ DolLinkList DL; InitDolLinkList(&DL); CreateDolLinkList_TailInsert(DL,5); printf("the length of list is %d\n",Length_DolLinkList(DL)); DisplayDolLinkList(DL); int r1,r2; if(GetElement_DolLinkList(DL,3,&r1)) printf("the element of postion 3 is %d\n",r1); else printf("Error:getElement:position\n"); if(GetElement_DolLinkList(DL,6,&r2)) printf("the element of postion 6 is %d\n",r2); else printf("Error:getElement:position\n"); if(LocateElement_DolLinkList(DL,4,&r1)) printf("the position of element 4 is %d\n",r1); else printf("Error:LocateElement:value\n"); if(LocateElement_DolLinkList(DL,8,&r1)) printf("the position of element 8 is %d\n",r1); else printf("Error:LocateElement:value\n"); if(GetPriorElement_DolLinkList(DL,6,&r1)) printf("before the 6 is %d\n",r1); else printf("Error:GetPriorElement:currentElement\n"); if(GetPriorElement_DolLinkList(DL,1,&r2)) printf("before the 1 is %d\n",r2); else printf("Error:GetPriorElement:currentElement\n"); if(GetNextElement_DolLinkList(DL,2,&r1)) printf("next the 2 is %d\n",r1); else printf("Error:nextElement:currentElement\n"); if(GetNextElement_DolLinkList(DL,5,&r1)) printf("next the 5 is %d\n",r1); else printf("Error:nextElement:currentElement\n"); Insert_DolLinkList(DL,3,666); Insert_DolLinkList(DL,7,999); Insert_DolLinkList(DL,1,111); Insert_DolLinkList(DL,9,2222); DisplayDolLinkList(DL); Delete_DolLinkList(DL,2,&r1); Delete_DolLinkList(DL,8,&r1); DisplayDolLinkList(DL); return 0; }