日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

删除两个双向链表中值相同的结点--带空白头结点

發布時間:2023/12/9 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 删除两个双向链表中值相同的结点--带空白头结点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有兩個雙向鏈表,空白頭結點為:ListA和ListB,要求刪除這兩個鏈表中關鍵字相同的結點, C語言實現,結點結構如下:

view plain
  • struct?node?????????????????????????????//?雙向鏈表結點??
  • {??
  • ????int?key;??
  • ????struct?node?*front,?*next;??
  • }; ?
  • ?完整代碼:

    /*功能:刪除兩個雙向鏈表(都帶空白頭結點)中值(key)相同的結點,規定header.front始終指向尾結點名詞定義:空白頭結點:該結點不作為真正保存值的結點;其front成員規定指向尾結點, next指向第一個非空白結點;當該鏈表無結點時,規定其front和next都為NULL.頭結點: 鏈表的第一個非空白頭結點(第一個元素)尾結點: 鏈表的最后一個非空白結點 */#include <stdio.h> #include <stdlib.h> #include <string.h>struct node // 雙向鏈表結點 {int key;struct node *front, *next; };/*功能: 創建雙向鏈表(尾插法:新創建的結點放在鏈表尾部)返回值:1-創建成功,0-創建失敗header: 創建的雙向鏈表的頭指針n: 待創建的結點個數 */ int createLinklist(struct node *header, int n) {int v;struct node *p = NULL;printf("請輸入%d個整數:\n", n);while(n-- > 0){scanf("%d", &v);p = malloc(sizeof(struct node));if(p){if(header->next == NULL)header->next = p; // 設置鏈表頭指針else{header->front->next = p;p->front = header->front;}p->key = v;p->next = header->next; // 新結點的next指向第一個結點header->next->front = p; // 結點的front指向新結點(作為最后一個結點)header->front = p; // header->front始終指向最后一個結點}elsereturn 0; // 創建鏈表失敗}return 1; // 創建鏈表成功 }// 輸出雙向鏈表中的值 void displayLinklist(struct node *header) {struct node *p = header->next;printf("header.front = 0x%X, header.next = 0x%X\n", header->front, header->next);if(NULL != p){do{printf("[p = 0x%X]\tdata = %d, front = 0x%X, next = 0x%X\n", p, p->key, p->front, p->next);p = p->next;}while(p != header->next);printf("\n");} }// 刪除雙向鏈表中所有結點并釋放空間(頭刪法) void FreeLinklist(struct node *header) {struct node *p;while(header->next){p = header->next; // p指向待刪結點if(p == header->front) // 待刪除的是尾結點{header->front = header->next = NULL;}else{header->front->next = p->next;p->next->front = header->front;header->next = p->next;}free(p);} }/*功能: 刪除雙向鏈表(頭指針pHeader)中值與key相同的結點,從結點*pStart開始向后搜索返回值: 如果從雙向鏈表中刪除了值為指定key的結點,返回1,否則返回0pHeader: 雙向鏈表空白頭指針pStart: 從*pStart開始向后搜索,刪除值與key相同的結點,直到遇到pHeader->nextkey: 待刪結點關鍵字注意: 調用此函數時,輸入參數pHeader==*pStart,程序將會出錯 */ int removeNode(struct node *pHeader, struct node **pStart, int key) {struct node *p, *temp;int del = 0;p = *pStart;*pStart = NULL;while(p){if(p->key == key){del = 1;temp = p; // temp指向待刪結點if(pHeader->next == p) // 刪除頭結點{if(pHeader->front == pHeader->next) // 待刪結點是鏈表的唯一結點pHeader->front = pHeader->next = NULL;else{ pHeader->front->next = p->next;p->next->front = pHeader->front; // 尾結點的next指向新的頭結點pHeader->next = p->next;}p = pHeader->next; // p指向新的頭結點}else{p->front->next = p->next;p->next->front = p->front;if(p == pHeader->front) // 待刪除的是尾結點pHeader->front = p->front; // pHeader->front指向新的尾結點p = p->next; // p指向被刪除結點的后繼結點}free(temp);}else{if(*pStart == NULL)*pStart = p;p = p->next;}if(*pStart && p == pHeader->next)break;}return del; }// 刪除兩個鏈表中值相同的結點 void removeEqualNodes(struct node *pHeadA, struct node *pHeadB) {struct node *p1, *p2;int del = 0;p1 = pHeadA->next;while(p1){p2 = pHeadB->next;if((del = removeNode(pHeadB, &p2, p1->key)) == 1){removeNode(pHeadA, &p1, p1->key);}elsep1 = p1->next;if(pHeadB->next == NULL || pHeadA->next == NULL || (del == 0 && p1 == pHeadA->next))break;} }int main(int argc, char *argv[]) {struct node listA, listB; // 定義兩個雙向鏈表的空白頭結點int n1, n2; // 保存待創建的鏈表結點個數if(argc < 3){printf("Usage: %s <n1> <n2>\n", argv[0]);return 1;}n1 = atoi(argv[1]);n2 = atoi(argv[2]);listA.front = listA.next = NULL;listB.front = listB.next = NULL;createLinklist(&listA, n1); // 創建雙向鏈表createLinklist(&listB, n2);printf("Before remove:\n");displayLinklist(&listA); // 顯示雙向鏈表內容displayLinklist(&listB);removeEqualNodes(&listA, &listB); // 刪除兩個鏈表中值相同的結點printf("\nAfter remove:\n");displayLinklist(&listA);displayLinklist(&listB);FreeLinklist(&listA); // 釋放空間FreeLinklist(&listB);return 0; }
    運行結果:

    E:\Program\VC\del\Debug>del.exe 2 4 請輸入2個整數: 14 25 請輸入4個整數: 14 25 63 66 Before remove: header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3808C0, header.next = 0x380818 [p = 0x380818] data = 14, front = 0x3808C0, next = 0x380850 [p = 0x380850] data = 25, front = 0x380818, next = 0x380888 [p = 0x380888] data = 63, front = 0x380850, next = 0x3808C0 [p = 0x3808C0] data = 66, front = 0x380888, next = 0x380818After remove: header.front = 0x0, header.next = 0x0 header.front = 0x3808C0, header.next = 0x380888 [p = 0x380888] data = 63, front = 0x3808C0, next = 0x3808C0 [p = 0x3808C0] data = 66, front = 0x380888, next = 0x380888E:\Program\VC\del\Debug>del.exe 2 4 請輸入2個整數: 14 25 請輸入4個整數: 12 23 21 54 Before remove: header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3808C0, header.next = 0x380818 [p = 0x380818] data = 12, front = 0x3808C0, next = 0x380850 [p = 0x380850] data = 23, front = 0x380818, next = 0x380888 [p = 0x380888] data = 21, front = 0x380850, next = 0x3808C0 [p = 0x3808C0] data = 54, front = 0x380888, next = 0x380818After remove: header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3808C0, header.next = 0x380818 [p = 0x380818] data = 12, front = 0x3808C0, next = 0x380850 [p = 0x380850] data = 23, front = 0x380818, next = 0x380888 [p = 0x380888] data = 21, front = 0x380850, next = 0x3808C0 [p = 0x3808C0] data = 54, front = 0x380888, next = 0x380818E:\Program\VC\del\Debug>del.exe 0 0 請輸入0個整數: 請輸入0個整數: Before remove: header.front = 0x0, header.next = 0x0 header.front = 0x0, header.next = 0x0After remove: header.front = 0x0, header.next = 0x0 header.front = 0x0, header.next = 0x0E:\Program\VC\del\Debug>del.exe 0 2 請輸入0個整數: 請輸入2個整數: 12 54 Before remove: header.front = 0x0, header.next = 0x0 header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 12, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 54, front = 0x3807A8, next = 0x3807A8After remove: header.front = 0x0, header.next = 0x0 header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 12, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 54, front = 0x3807A8, next = 0x3807A8E:\Program\VC\del\Debug>del.exe 2 0 請輸入2個整數: 14 25 請輸入0個整數: Before remove: header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x0, header.next = 0x0After remove: header.front = 0x3807E0, header.next = 0x3807A8 [p = 0x3807A8] data = 14, front = 0x3807E0, next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x0, header.next = 0x0E:\Program\VC\del\Debug>del.exe 1 1 請輸入1個整數: 25 25 請輸入1個整數: Before remove: header.front = 0x3807A8, header.next = 0x3807A8 [p = 0x3807A8] data = 25, front = 0x3807A8, next = 0x3807A8header.front = 0x3807E0, header.next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807E0, next = 0x3807E0After remove: header.front = 0x0, header.next = 0x0 header.front = 0x0, header.next = 0x0E:\Program\VC\del\Debug>del.exe 5 6 請輸入5個整數: 14 25 63 47 58 14 請輸入6個整數: 25 36 47 55 25 Before remove: header.front = 0x380888, header.next = 0x3807A8 [p = 0x3807A8] data = 14, front = 0x380888, next = 0x3807E0 [p = 0x3807E0] data = 25, front = 0x3807A8, next = 0x380818 [p = 0x380818] data = 63, front = 0x3807E0, next = 0x380850 [p = 0x380850] data = 47, front = 0x380818, next = 0x380888 [p = 0x380888] data = 58, front = 0x380850, next = 0x3807A8header.front = 0x3809D8, header.next = 0x3808C0 [p = 0x3808C0] data = 14, front = 0x3809D8, next = 0x3808F8 [p = 0x3808F8] data = 25, front = 0x3808C0, next = 0x380930 [p = 0x380930] data = 36, front = 0x3808F8, next = 0x380968 [p = 0x380968] data = 47, front = 0x380930, next = 0x3809A0 [p = 0x3809A0] data = 55, front = 0x380968, next = 0x3809D8 [p = 0x3809D8] data = 25, front = 0x3809A0, next = 0x3808C0After remove: header.front = 0x380888, header.next = 0x380818 [p = 0x380818] data = 63, front = 0x380888, next = 0x380888 [p = 0x380888] data = 58, front = 0x380818, next = 0x380818header.front = 0x3809A0, header.next = 0x380930 [p = 0x380930] data = 36, front = 0x3809A0, next = 0x3809A0 [p = 0x3809A0] data = 55, front = 0x380930, next = 0x380930E:\Program\VC\del\Debug>

    總結

    以上是生活随笔為你收集整理的删除两个双向链表中值相同的结点--带空白头结点的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。