資源描述:
《雙向循環(huán)鏈表56405》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、#include#includetypedefstructlist{intdata;structlist*left;structlist*right;}LIST;LIST*init_L(void){LIST*head=(LIST*)malloc(sizeof(LIST));if(head==NULL){printf("鍐呭瓨鍒嗛厤澶辮觸錛乗n");return0;}head->left=head->right=head;returnhead;}LIST*insert_L(LIST
2、*head,intvalue){LIST*p,*newnode;newnode=(LIST*)malloc(sizeof(LIST));if(head==NULL){printf("Headisempty!");}if(newnode==NULL){printf("鍐呭瓨鍒嗛厤澶辮觸錛乗n");return0;}p=head;newnode->data=value;while(p->right!=head)//閬嶅巻鍒版湯灝?{p=p->right;}/*************鎻掑叆鎿嶄綔***********
3、********//**/newnode->right=p->right;/**//**/p->right=newnode;/**//**/newnode->left=p;/**//**************************************/returnhead;}voidprint(LIST*head){inti=0;LIST*p;if(head==NULL){printf("絀洪摼琛紒");}p=head->right;while(p!=head){++i;printf("%4dt",p
4、->data);p=p->right;}printf("****************姝ら摼琛ㄦ湁%d涓暟鎹紒",i);}intdelete(LIST*head,intnum){LIST*p;inti=1;if(head==NULL){printf("絀洪摼琛紒");}p=head->right;while(p!=head){if(p->data==num){p->left->right=p->right;p->right->left=p->left;printf("瑕佸垹闄ょ殑鏁版嵁%d鏄
5、摼琛ㄧ殑絎?%d涓暟鎹?宸插垹闄?",num,i);print(head);break;}/*if(p->data!=num&&p->right==head){printf("瑕佸垹闄ょ殑鏁版嵁%d鏃犳硶鎵懼埌!",num);break;}*/p=p->right;i++;}if(p->data!=num)//&&p->right==head{printf("瑕佸垹闄ょ殑鏁版嵁%d鏃犳硶鎵懼埌----------------------",num);}return0;}voidfind_L(LIST*h
6、ead,intfind_num){inti=1;LIST*p;if(head==NULL){printf("絀洪摼琛ㄦ病鏈夋暟鎹?");}p=head;p=p->right;while(p!=head){if(p->data==find_num){printf("瑕佹煡鎵劇殑鏁版嵁%d鏄摼琛ㄧ殑絎?%d涓暟鎹紒",find_num,i);break;}i++;p=p->right;}if(p->data!=find_num&&p->right==head){printf("瑕佹煡鎵劇殑鏁版嵁%d涓嶅瓨鍦
7、紒",find_num);}}intmain(void){LIST*head=NULL;head=init_L();insert_L(head,5);insert_L(head,15);insert_L(head,25);insert_L(head,215);print(head);delete(head,25);find_L(head,5);delete(head,5);delete(head,15);delete(head,22);insert_L(head,33);find_L(head,33);prin
8、t(head);return0;}