//函數(shù)聲明:#include"cirtwowaylinklist.h" #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct CirDulNode { struct CirDulNode *prior; ElemType data; struct CirDulNode *next; }DulNode; void judgement_NULL(DulNode *head); DulNode * creat(); //創(chuàng)建鏈表 void insert(DulNode *head, int i, ElemType x); //插入結(jié)點(diǎn) void delete_element(DulNode *head, ElemType x); //刪除鏈表中所有值為X的結(jié)點(diǎn) void delete_place(DulNode *head, int i); //刪除鏈表中第i個結(jié)點(diǎn) void find_place(DulNode *head, ElemType x); //尋找鏈表中所有x,所在的結(jié)點(diǎn)位置 void find_element(DulNode *head, int i); //尋找第i個結(jié)點(diǎn)上的值 void length(DulNode *head); void initslinklist(DulNode *head); //釋放整個鏈表 void sortrank(DulNode *head); //對鏈表進(jìn)行排序 void output_order(DulNode *head); //正序打印 void output_back(DulNode *head); //逆序打印 int means(); //選擇方式 //函數(shù)實(shí)現(xiàn):#include"cirtwowaylinklist.c" #include"cirtwowaylinklist.h" void judgement_NULL(DulNode *head) //判斷動態(tài)內(nèi)存是否開辟成功 { if (head == NULL) { perror("out of memory\n"); exit(EXIT_FAILURE); } } DulNode * creat() { DulNode *head, *p, *r; ElemType x; head = (DulNode *)malloc(sizeof(DulNode)); judgement_NULL(head); r = head; printf("開始建立:"); while (1) { scanf("%d", &x); if (x != 0) //建立鏈表,以0作為結(jié)束標(biāo)志 { p = (DulNode *)malloc(sizeof(DulNode)); judgement_NULL(p); p->data = x; r->next = p; p->prior = r; r = p; } else break; } head->prior = r; //將鏈表首尾鏈接 r->next = head; printf("創(chuàng)建成功\n"); return head; } void insert(DulNode *head, int i, ElemType x) //在i位置上插入一個X,因?yàn)殒湵硌h(huán),所以i>1就一定能找到一個位置 { DulNode *p, *r; p = (DulNode *)malloc(sizeof(DulNode)); judgement_NULL(p); p->data = x; r = head; while (1) { if (i <= 1) break; r = r->next; i--; } if (i < 1) printf("沒有該結(jié)點(diǎn)\n"); else { p->next = r->next; //在頭結(jié)點(diǎn)之前,之后都可以插入 p->prior = r; r->next = p; p->next->prior = p; } } void delete_element(DulNode *head, ElemType x) //刪除鏈表中所有的x元素所在的結(jié)點(diǎn) { DulNode *p, *r; int count = 0; p = head->next; while (p != head) { if (p->data == x) { count++; r = p; p = p->next; r->prior->next = r->next; r->next->prior = r->prior; free(r); r = NULL; } else p = p->next; } if (count == 0) printf("鏈表中沒有此元素\n"); else printf("共刪除%d個\n", count); } void delete_place(DulNode *head, int i) //刪除一個指定位置的結(jié)點(diǎn),結(jié)點(diǎn)位置i>0 { DulNode *p; p = head; while (1) { if (i <= 1) break; p = p->next; i--; } if (i < 1) printf("沒有該結(jié)點(diǎn)\n"); else { if (p->next = head) //如果要刪除的結(jié)點(diǎn)是頭結(jié)點(diǎn)則跳過刪除頭結(jié)點(diǎn)之后的結(jié)點(diǎn) { p = head->next; head->next = p->next; p->next->prior = head; } else { p->prior->next = p->next; p->next->prior = p->prior; free(p); } } } void find_place(DulNode *head, ElemType x) //找到鏈表中所有的x元素所在的結(jié)點(diǎn)位置 { int count = 0; int flag = 0; DulNode *p; p = head->next; while (p != head) { count++; if (p->data == x) { flag++; printf("此元素結(jié)點(diǎn)位置:%d\n", count); } p = p->next; } if (flag == 0) printf("鏈表中沒有此元素\n"); } void find_element(DulNode *head, int i) //找到i結(jié)點(diǎn)上的元素,并輸出 { DulNode *p; p = head->next; while (p != head) { if (i <= 1) break; p = p->next; i--; } if (p == head || i != 1) printf("沒有該結(jié)點(diǎn)\n"); else printf("此結(jié)點(diǎn)元素:%d\n", p->data); } void length(DulNode *head) { int len = 0; DulNode *p; p = head->next; while (p != head) { len++; p = p->next; } printf("鏈表長度:%d\n", len); } void initslinklist(DulNode *head) //釋放整個鏈表 { DulNode *p; p = head->next; while (p != head) { p = p->next; free(p->prior); p->prior = NULL; } free(head); head = NULL; p = NULL; } void sortrank(DulNode *head) //對整個鏈表進(jìn)行排序 { DulNode *p, *r; ElemType tmp; p = head->next; while (p->next!= head) { r = head->next; while (r->next!= head) { if ((r->data) >(r->next->data)) { tmp = r->data; r->data = r->next->data; r->next->data = tmp; } r = r->next; } p=p->next; } printf("排序成功\n"); } void output_order(DulNode *head) //正向打印整個鏈表 { DulNode *p; p = head; if (head->next == head) printf("鏈表為空\n"); else { printf("正序打印鏈表:"); p = p->next; while (p != head) { printf("%d ", p->data); p = p->next; } printf("\n"); } } void output_back(DulNode *head) //反向打印整個鏈表 { DulNode *p; p = head->prior; if (head->next == head) printf("鏈表為空\n"); else { printf("反向打印鏈表:"); while (p != head) { printf("%d ", p->data); p = p->prior; } printf("\n"); } } int means() //選擇以哪種方式進(jìn)行操作 { int m = 0; while (1) { printf("請選擇方式:"); scanf("%d", &m); if (m == 1 || m == 2) break; printf("選擇無效,請重新選擇\n"); } return m; } //函數(shù)測試: #include"cirtwowaylinklist.h" int main() { DulNode *ret = NULL; int n = 0; int i = 0; ElemType x; printf("*********************************************\n"); printf("*********************************************\n"); printf("*1.CreatLinkList 2.Insert **********\n"); printf("*3.Delete 4.Find **********\n"); printf("*5.Length 6.Output **********\n"); printf("*7.InitsLinkLinst 8.Sortrank ********\n"); printf("*0.Exit *******************\n\n\n"); while (1) { printf("請選擇功能:"); scanf("%d", &n); if (n == 0) //選擇0直接退出 { free(ret); //退出前先釋放列表 exit(1); } if (ret == NULL) //如果ret為空,則首先建立鏈表 { if (n == 1) { printf("創(chuàng)建鏈表以0作為結(jié)束標(biāo)志\n"); ret = creat(); } else printf("請先建立鏈表\n"); } else { switch (n) //選擇剩下的功能 { case 1: //當(dāng)ret不為空時不能建立鏈表 printf("當(dāng)前鏈表未結(jié)束,請先初始化鏈表\n"); break; case 2: printf("請輸入要插入的元素和位置:"); scanf("%d", &x); scanf("%d", &i); insert(ret, i, x); break; case 3: printf("*1.delete_element 2.delete_place*\n"); if (means() == 1) { printf("請輸入要刪除的元素:"); scanf("%d", &x); delete_element(ret, x); } else { printf("請輸入要刪除的結(jié)點(diǎn):"); scanf("%d", &i); delete_place(ret, i); } break; case 4: printf("*1.find_place 2.find_element*\n"); if (means() == 1) { printf("請輸入要查找的元素:"); scanf("%d", &x); find_place(ret, x); } else { printf("請輸入要查找的位置:"); scanf("%d", &i); find_element(ret, i); } break; case 5: length(ret); break; case 6: printf("*1.output_order 2.output_back*\n"); if (means() == 1) output_order(ret); else output_back(ret); break; case 7: //將當(dāng)前鏈表釋放 initslinklist(ret); ret = NULL; break; case 8: sortrank(ret); break; default: printf("選擇無效,請重新選擇\n"); break; } //switch()語句結(jié)束 } n = 0; } //循環(huán)結(jié)束 system("pause"); return 0; }
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)尖草坪,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
網(wǎng)站欄目:雙向循環(huán)單鏈表
URL地址:http://vcdvsql.cn/article28/phopjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、、網(wǎng)站收錄、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)