一璧南、雙向循環(huán)鏈表的結點及數據定義
#define ERROR0
#define TRUE1
#define FALSE0
#define OK1
#define MAXSIZE20/* 存儲空間初始分配量 */
typedef int Status;/* Status是函數的類型,其值是函數結果狀態(tài)代碼掌逛,如OK等 */
typedef int ElemType;/* ElemType類型根據實際情況而定,這里假設為int */
//定義結點
typedef struct Node{
? ? structNode*prior;//前驅
? ? ElemTypedata;//數據
? ? structNode*next;//后續(xù)
}Node;
typedefstructNode* LinkList;
二、雙向循環(huán)鏈表初始化
Status CreateList(LinkList *L){
? ? *L = (LinkList)malloc(sizeof(Node));
? ? if(*L ==NULL) {
? ? ? ? returnERROR;
? ? }
? ? (*L)->next= (*L);
? ? (*L)->prior= (*L);
? ? (*L)->data= -1;
? ? //指向頭結點
? ? LinkListp = *L;
//? ? LinkList temp;
? ? for(inti=0; i<10; i++) {
? ? ? ? //新增數據
? ? ? ? LinkListtemp = (LinkList)malloc(sizeof(Node));
? ? ? ? if(temp ==NULL) {
? ? ? ? ? ? returnERROR;
? ? ? ? }
? ? ? ? temp->data= i;
? ? ? ? //為新增的結點建立雙向鏈表關系
? ? ? ? //temp 是p的后繼
? ? ? ? p->next= temp;
? ? ? ? //temp 的前驅是p
? ? ? ? temp ->prior= p;
? ? ? ? //
? ? ? ? temp->next= *L;
? ? ? ? //
? ? ? ? (*L)->prior= temp;
? ? ? ? //p 要記錄最后的結點的位置,方便下一次插入
? ? ? ? p = p->next;
? ? }
//? ? //最后一個結點的next指向頭結點,頭結點的prior指向最后一個結點
//? ? p->next = *L;
//? ? (*L)->prior = p;
? ? returnOK;
}
三、打印循環(huán)鏈表的元素
void display(LinkList L){
? ? if(L ==NULL) {
? ? ? ? printf("打印的雙向鏈表為空!\n");
? ? ? ? return;
? ? }
? ? LinkListp = L->next;
? ? while(p != L) {
? ? ? ? printf("%d ",p->data);
? ? ? ? p = p->next;
? ? }
? ? printf("\n");
}
四邀跃、雙向循環(huán)鏈表插入元素
/*當插入位置超過鏈表長度則插入到鏈表末尾*/
Status LinkListInsert(LinkList *L,int index,ElemType data){
? ? if(*L ==NULL|| index <1) {
? ? ? ? returnERROR;
? ? }
? ? //指向頭結點
? ? LinkListp = *L;
? ? //找到要插入的數據的前一個結點
? ? inti =1;
? ? while(inext!= *L) {
? ? ? ? p = p->next;
? ? ? ? i++;
? ? }
? ? if(i>index) {
? ? ? ? returnERROR;
? ? }
?? //
? ? LinkList temp = (LinkList)malloc(sizeof(Node));
? ? if(temp ==NULL) {
? ? ? ? returnERROR;
? ? }
? ? temp->data= data;
? ? //
? ? temp->next= p->next;
? ? //最后一個結點
? ? if(temp->next== *L) {
? ? ? ? (*L)->prior= temp;
? ? }else{
? ? ? ? p->next->prior= temp;
? ? }
? ? temp->prior= p;
? ? //
? ? p->next= temp;
//? ? p->next->prior = temp;
? ? returnOK;
}
五竿刁、雙向循環(huán)鏈表刪除結點
Status deleteList(LinkList *L,int index,ElemType *data){
? ? if(*L ==NULL|| index <1) {
? ? ? ? returnERROR;
? ? }
? ? LinkListtemp = (*L)->next;
? ? if(*L == temp) {
? ? ? ? returnERROR;
? ? }
? ? inti=1;
? ? while(inext!= *L)) {
? ? ? ? temp = temp->next;
//? ? ? ? NSLog(@"temp data = %d",temp->data);
? ? ? ? i++;
? ? }
? ? *data = temp->data;
? ? temp->prior->next= temp->next;
? ? temp->next->prior= temp->prior;
? ? free(temp);
? ? returnOK;
}
六息楔、查詢雙向鏈表的數據
ElemType SelectList(LinkList L,int index){
? ? if(L ==NULL|| index<1) {
? ? ? ? return-1;
? ? }
? ? LinkListp = L->next;
? ? inti=1;
? ? while(inext!= L) {
? ? ? ? p = p->next;
? ? ? ? i++;
? ? }
? ? if(p == L) {
? ? ? ? return-1;
? ? }
? ? returnp->data;
? ? return-1;
}
七、修改雙向鏈表的數據
Status ReplaceList(LinkList L,int index,ElemType data){
? ? if(L ==NULL|| index<1) {
? ? ? ? returnERROR;
? ? }
? ? LinkListp = L->next;
? ? inti=1;
? ? while(inext!=L) {
? ? ? ? p = p->next;
? ? ? ? i++;
? ? }
? ? if(p==L) {
? ? ? ? returnERROR;
? ? }
? ? p->data= data;
? ? returnOK;
}
八、調用
intmain(intargc,constchar* argv[]) {
? ? LinkList L;
? ? CreateList(&L);
? ? display(L);
? ? inttemp,item;
? ? printf("輸入要插入的位置和數據用空格隔開:");
? ? scanf("%d %d",&temp,&item);
? ? LinkListInsert(&L,temp,item);
? ? display(L);
? ? printf("輸入要刪除位置1:");
? ? scanf("%d",&temp);
? ? deleteList(&L, temp, &item);
? ? display(L);
? ? printf("輸入要刪除位置2:");
? ? scanf("%d",&temp);
? ? deleteList(&L, temp, &item);
? ? display(L);
? ? printf("輸入要刪除位置3:");
? ? scanf("%d",&temp);
? ? deleteList(&L, temp, &item);
? ? display(L);
? ? printf("輸入要查詢的位置1:");
? ? scanf("%d",&temp);
? ? ElemTypedata =SelectList(L, temp);
? ? printf("查詢到的數據是:%d\n",data);
? ? printf("輸入要查詢的位置2:");
? ? scanf("%d",&temp);
? ? data =SelectList(L, temp);
? ? printf("查詢到的數據是:%d\n",data);
? ? printf("輸入要查詢的位置3:");
? ? scanf("%d",&temp);
? ? data =SelectList(L, temp);
? ? printf("查詢到的數據是:%d\n",data);
? ? //
? ? printf("輸入要修改的位置和數據1:");
? ? scanf("%d %d",&temp,&item);
? ? ReplaceList(L,temp,item);
? ? display(L);
? ? printf("輸入要修改的位置和數據2:");
? ? scanf("%d %d",&temp,&item);
? ? ReplaceList(L,temp,item);
? ? display(L);
? ? printf("輸入要修改的位置和數據3:");
? ? scanf("%d %d",&temp,&item);
? ? ReplaceList(L,temp,item);
? ? display(L);
? ? return0;
}