您好,登錄后才能下訂單哦!
//函數聲明:#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(); //創建鏈表 void insert(DulNode *head, int i, ElemType x); //插入結點 void delete_element(DulNode *head, ElemType x); //刪除鏈表中所有值為X的結點 void delete_place(DulNode *head, int i); //刪除鏈表中第i個結點 void find_place(DulNode *head, ElemType x); //尋找鏈表中所有x,所在的結點位置 void find_element(DulNode *head, int i); //尋找第i個結點上的值 void length(DulNode *head); void initslinklist(DulNode *head); //釋放整個鏈表 void sortrank(DulNode *head); //對鏈表進行排序 void output_order(DulNode *head); //正序打印 void output_back(DulNode *head); //逆序打印 int means(); //選擇方式 //函數實現:#include"cirtwowaylinklist.c" #include"cirtwowaylinklist.h" void judgement_NULL(DulNode *head) //判斷動態內存是否開辟成功 { 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作為結束標志 { 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("創建成功\n"); return head; } void insert(DulNode *head, int i, ElemType x) //在i位置上插入一個X,因為鏈表循環,所以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("沒有該結點\n"); else { p->next = r->next; //在頭結點之前,之后都可以插入 p->prior = r; r->next = p; p->next->prior = p; } } void delete_element(DulNode *head, ElemType x) //刪除鏈表中所有的x元素所在的結點 { 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) //刪除一個指定位置的結點,結點位置i>0 { DulNode *p; p = head; while (1) { if (i <= 1) break; p = p->next; i--; } if (i < 1) printf("沒有該結點\n"); else { if (p->next = head) //如果要刪除的結點是頭結點則跳過刪除頭結點之后的結點 { 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元素所在的結點位置 { int count = 0; int flag = 0; DulNode *p; p = head->next; while (p != head) { count++; if (p->data == x) { flag++; printf("此元素結點位置:%d\n", count); } p = p->next; } if (flag == 0) printf("鏈表中沒有此元素\n"); } void find_element(DulNode *head, int i) //找到i結點上的元素,并輸出 { DulNode *p; p = head->next; while (p != head) { if (i <= 1) break; p = p->next; i--; } if (p == head || i != 1) printf("沒有該結點\n"); else printf("此結點元素:%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) //對整個鏈表進行排序 { 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() //選擇以哪種方式進行操作 { int m = 0; while (1) { printf("請選擇方式:"); scanf("%d", &m); if (m == 1 || m == 2) break; printf("選擇無效,請重新選擇\n"); } return m; } //函數測試: #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("創建鏈表以0作為結束標志\n"); ret = creat(); } else printf("請先建立鏈表\n"); } else { switch (n) //選擇剩下的功能 { case 1: //當ret不為空時不能建立鏈表 printf("當前鏈表未結束,請先初始化鏈表\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("請輸入要刪除的結點:"); 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: //將當前鏈表釋放 initslinklist(ret); ret = NULL; break; case 8: sortrank(ret); break; default: printf("選擇無效,請重新選擇\n"); break; } //switch()語句結束 } n = 0; } //循環結束 system("pause"); return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。