昨天写完了,线性表的顺序表示,今天有琢磨了一天的链表,刚开始准备写一个普通的链表,后来想象这也太没挑战,干脆来一个双向循环聊表把。有挑战,我喜欢,好啦,不多说了,我把今天的代码贴出来和大家一起来分享把!
如果代码中有什么错误,希望您不吝赐教,您可以把您的意见发送到yijiyong100@163.com,我会尽快给您回复的。
C语言 双向循环链表的实现和结构 线性表的链式实现
/****************************************/ /*Desciption: Doubleaction Loop Link List*/ /*Email:yijiyong100@163.com*/ /*Author:yi_landry Haerbin Normal University Computer Science*/ /*Date:2008-4-30*/ /*Copyright:HNU2008.cop*/ /*Environment:turbo c 2.01 English Version*/ /****************************************/ # include<stdlib.h> # include<stdio.h> # define OVERFLOW 0 # define OK 1 /****************************************/ /*The struct of node of doubleacation loop link list */ /****************************************/ typedef struct DuNode { int item; struct DuNode * next; struct DuNode * prior; }DuNode; /****************************************/ /*The struct of doubleaction loop link list*/ /****************************************/ struct DuList { struct DuNode * head;/*the head pointer of the doubleaction loop link list*/ int length;/*the length of the doubleaction loop link list*/ }L; /****************************************/ /*Initial the list*/ /****************************************/ InitList(struct DuList * L) { L->head = (DuNode *)malloc(sizeof(DuNode)); if (!L->head) exit(OVERFLOW); L->head->next = L->head; L->head->prior = L->head; L->length = 0; printf("Initial the doubleaction loop link list successfully!/n"); } /****************************************/ /*Insert a element into the list*/ /****************************************/ ListInsert(struct DuList * L,int i,int elm) { struct DuNode * p,* insertNode; int count = 0; p = (DuNode *)malloc(sizeof(DuNode)); insertNode = (DuNode *)malloc(sizeof(DuNode)); if (i<=0 || i>L->length+1) { printf("The location %d you input not valid!/n",i); } else if (L->length ==0) { insertNode->item = elm; p = L->head; p->next = p->prior=insertNode; insertNode->next = insertNode->prior = p; L->length++; printf("The new node %d was inserted into the list at the location of %d successfully!/n",elm,i); } else { p = L->head; while (1) { if (count == i) break; p = p->next; count++; } insertNode->item = elm; insertNode->prior = p ->prior; p->prior->next = insertNode; insertNode->next = p; p->prior = insertNode; L->length++; printf("The new node %d was inserted into the list at the location of %d successfully/n",elm,i); } } /****************************************/ /*Delete the element at the location i in the list*/ /****************************************/ ListDelete(struct DuList * L,int i) { DuNode * p; int deleteElm,count=0; p = L->head; while (1) { if (count == i) break; p = p->next; count++; } deleteElm = p->item; p->prior->next = p->next; p->next->prior = p->prior; printf("Delete the node %d at the location of %d successfully!/n",deleteElm,i); } /****************************************/ /*Judge the list is empty or not*/ /****************************************/ ListEmpty(struct DuList * L) { if (L->length==0) { printf("The list now is empty!/n"); } else { printf("The list now is not empty!/n"); PrintList(L); } } /****************************************/ /*Get the length of the list*/ /****************************************/ ListLength(struct DuList * L) { printf("The length of current list is %d/n",L->length); } /****************************************/ /*Destroy the list and free the memory distributed at first*/ /****************************************/ DestroyList(struct DuList *L) { free(L->head); L->length = 0; printf("Destroy the list successfully!/n"); } /****************************************/ /*Clear all the elements the list*/ /****************************************/ ClearList(struct DuList * L) { L->head->prior=L->head; L->head->next = L->head; L->length =0; printf("Clear the list successfully!/n"); } /****************************************/ /*Get the element at tbe location i */ /****************************************/ GetElement(struct DuList * L,int i) { DuNode * p; int count=0; p = L->head; while (1) { if (count == i) break; p = p->next; count++; } printf("The node at the location %d is %d/n",i,p->item); } /****************************************/ /*Find a number if or not in the list*/ /****************************************/ FindElement(struct DuList * L,int elm) { DuNode * p; int totalFind=0,count=0; p = L->head; while (1) { if (count == L->length)break; p = p->next; count++; if (p->item == elm) { printf("We find %d at the location of %d in the list!/n",elm,count); totalFind++; } } if (totalFind == 0) { printf("We can not find %d in the list!/n",elm); } else { printf("We totally find %d <%d> in the list/n",totalFind,elm); } } /****************************************/ /*Get prior element of some element in the list*/ /****************************************/ PriorElement(struct DuList * L,int elm) { DuNode * p; int totalFind=0,count=0; p = L->head; while (1) { if (count == L->length)break; p = p->next; count++; if (p->item == elm) { if (p->prior != L->head) { printf("We find the prior of %d is %d at the location of %d in the list!/n",elm,p->prior->item,count-1); } else { printf("The prior of %d is the head node of the list!/n",elm); } totalFind++; } } if (totalFind == 0) { printf("We can not find the prior of %d in the list!/n",elm); } } /****************************************/ /*Get next element of some element in the list*/ /****************************************/ NextElement(struct DuList * L,int elm) { DuNode * p; int totalFind=0,count=0; p = L->head; while (1) { if (count == L->length)break; p = p->next; count++; if (p->item == elm) { if (p->next != L->head) { printf("We find the next of %d is %d at the location of %d in the list!/n",elm,p->next->item,count+1); } else { printf("The next of %d is the head node of the list!/n",elm); } totalFind++; } } if (totalFind == 0) { printf("We can not find the next of %d in the list!/n",elm); } } /****************************************/ /*Print the list */ /****************************************/ PrintList(struct DuList * L) { struct DuNode * p; p = L->head; if (L->length == 0) { printf("The current doubleaction loop link list is empty!/n"); } else if (L->length == 1) { printf("The current doubleaction loop link list :/n<=%d>",p->next->item); } else { printf("The current doubleaction loop link list:/n"); while(1) { if (p->next == L->head) break; p = p->next; printf("%d<=>",p->item); } printf("/n"); } } /****************************************/ /*Show a example to the user*/ /****************************************/ ListExample() { InitList(&L); ListInsert(&L,1,1); ListInsert(&L,2,2); ListInsert(&L,3,3); ListInsert(&L,4,4); ListInsert(&L,5,5); ListInsert(&L,6,6); ListInsert(&L,5,50); ListInsert(&L,6,60); ListInsert(&L,7,7); ListInsert(&L,8,8); ListInsert(&L,9,9); FindElement(&L,6); FindElement(&L,7); FindElement(&L,10); PriorElement(&L,6); PriorElement(&L,10); GetElement(&L,3); PrintList(&L); PrintList(&L); ListDelete(&L,4); PrintList(&L); ListDelete(&L,6); PrintList(&L); ListInsert(&L,18,9); ListLength(&L); DestroyList(&L); ListLength(&L); return 0; } /****************************************/ /*Show the help information to the user*/ /****************************************/ void help() { printf(" /*****************************************************************//n"); printf(" /*****************************************************************//n"); printf(" /* The Doubleaction Loop Link List Operation Version 1.0 *//n"); printf(" /* View the example information 1 *//n"); printf(" /* Insert a number into the list 2 *//n"); printf(" /* Delete a number from the list 3 *//n"); printf(" /* Find a number if or not in the list 4 *//n"); printf(" /* Get the prior of some number from the list 5 *//n"); printf(" /* Get the next of some number from the list 6 *//n"); printf(" /* Get the size of list 7 *//n"); printf(" /* Show the help information 8 *//n"); printf(" /* View current list information 9 *//n"); printf(" /* Exit Doubleaction Loop Link List Q *//n"); printf(" /*****************************************************************//n"); } main() { char userInput; int insertItem,insertLocation,deleteLocation,findItem,priorItem,nextItem; help(); InitList(&L); while(1) { printf("Slect the operation you want to do: input the correspond number!you can enter 1 to view the example ,and enter 8 to view the help./n"); scanf("%c",&userInput); switch(userInput) { case '1':ListExample();break; case '2': for(;;) { printf("Please input a integer and the location you want to insert into the list:/n"); printf("Forexample 1 1 if you do not want to insert a number into the list again,you can input 0 0/n"); scanf("%d %d",&insertItem,&insertLocation); if (insertItem == 0 && insertLocation == 0) break; ListInsert(&L,insertLocation,insertItem); PrintList(&L); } break; case '3': for(;;) { printf("Please input the location of the element you want to delete from the list:/n"); printf("Forexample 2.if you do not want to delete a number from the list again,you can input -1/n"); scanf("%d",&deleteLocation); if ( deleteLocation== -1) break; ListDelete(&L,deleteLocation); PrintList(&L); } break; case '4': for(;;) { printf("Please input a integer you want to find from the list:/n"); printf("Forexample 1 . if you do not want to find a number from the list again,you can input -1/n"); scanf("%d",&findItem); if (findItem == -1) break; FindElement(&L,findItem); PrintList(&L); } break; case '5': for(;;) { printf("Please input a integer you want to get the prior from the list:/n"); printf("Forexample 1. if you do not want to get the prior form the list again,you can input -1/n"); scanf("%d",&priorItem); if (priorItem == -1) break; PriorElement(&L,priorItem); PrintList(&L); } break; case '6': for(;;) { printf("Please input a integer you want to get the next from the list:/n"); printf("Forexample 1. if you do not want to get the next form the list again,you can input -1/n"); scanf("%d",&nextItem); if (nextItem == -1) break; NextElement(&L,nextItem); PrintList(&L); } break; case '7':ListLength(&L);break; case '8':help();break; case '9':PrintList(&L);break; case 'Q':break; case 'q':break; } if (userInput == 'q'|| userInput == 'Q') break; } return 0; }