删除两个双向链表中值相同的结点--无空白头结点
生活随笔
收集整理的這篇文章主要介紹了
删除两个双向链表中值相同的结点--无空白头结点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有兩個雙向鏈表,頭指針為:pListA和pListB,要求刪除這兩個鏈表中值相同的結點, C語言實現,結點結構如下:
struct node // 雙向鏈表結點 {int key;struct node *front, *next; };完整源代碼如下: /*功能:刪除兩個雙向鏈表(都不帶頭結點)中值(key)相同的結點 */#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 *pre, *p;*header = pre = p = NULL;printf("請輸入%d個整數:\n", n);while(n-- > 0){scanf("%d", &v);p = malloc(sizeof(struct node));if(p){if(*header == NULL)*header = p; // 設置鏈表頭指針else{pre->next = p; p->front = pre;}p->key = v;p->next = *header; // 新結點的next指向頭結點(*header)->front = p; // 更改頭結點的front成員pre = p;}elsereturn 0; // 創建鏈表失敗}return 1; // 創建鏈表成功 }// 輸出雙向鏈表中的值 void displayLinklist(struct node *header) {struct node *p = header;if(NULL != p){do{printf("[p = %X]\tdata = %d, front = %X, next = %X\n", p, p->key, p->front, p->next);p = p->next;}while(p != header);printf("\n");} }// 刪除雙向鏈表中所有結點并釋放空間(頭刪法) void FreeLinklist(struct node *header) {struct node *p;int i = 0;while(header){p = header;if(header->next == header) // 待刪除的是最后一個結點header = NULL;else{header->front->next = header->next;header->next->front = header->front;header = header->next;}free(p);} }/*功能: 刪除雙向鏈表(頭指針*pHeader)中值與key相同的結點,從結點*pStart開始向后搜索返回值: 如果從雙向鏈表中刪除了值為指定key的結點,返回1,否則返回0pHeader: 雙向鏈表頭指針pStart: 從*pStart開始向后搜索,刪除值與key相同的結點,直到遇到*pHeaderkey: 待刪除結點的值注意: 調用此函數時,輸入參數*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 == temp) // 刪除頭結點{if(*pHeader == temp->next) // 該頭結點是鏈表的最后一個結點*pHeader = NULL;else{ *pHeader = temp->next;p->front->next = *pHeader; // 最后一個結點的next指向新的頭結點p->next->front = p->front; // 被刪結點的后繼結點的front指向被刪結點的前驅}p = *pHeader; // p指向新的頭結點}else{p->front->next = p->next;p->next->front = p->front;p = p->next; // p指向被刪除結點的后繼結點}free(temp);}else{if(*pStart == NULL)*pStart = p;p = p->next;}if(*pStart && p == *pHeader)break;}return del; }// 刪除兩個鏈表中值相同的結點 void removeEqualNodes(struct node **pHeadA, struct node **pHeadB) {struct node *p1, *p2;int del = 0;p1 = *pHeadA;while(p1){p2 = *pHeadB;if((del = removeNode(pHeadB, &p2, p1->key)) == 1){removeNode(pHeadA, &p1, p1->key);}elsep1 = p1->next;if(*pHeadB == NULL || *pHeadA == NULL || (del == 0 && p1 == *pHeadA))break;} }int main(int argc, char *argv[]) {struct node *plistA = NULL, *plistB = NULL;int n1, n2;if(argc < 3){printf("Usage: %s <n1> <n2>\n", argv[0]);return 1;}n1 = atoi(argv[1]);n2 = atoi(argv[2]);createLinklist(&plistA, n1); // 創建雙向鏈表createLinklist(&plistB, n2);printf("Before remove:\n");displayLinklist(plistA); // 顯示雙向鏈表內容displayLinklist(plistB);removeEqualNodes(&plistA, &plistB); // 刪除兩個鏈表中值相同的結點printf("\nAfter remove:\n");displayLinklist(plistA);displayLinklist(plistB);FreeLinklist(plistA); // 釋放空間FreeLinklist(plistB);return 0; }
總結
以上是生活随笔為你收集整理的删除两个双向链表中值相同的结点--无空白头结点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 总统与乞丐
- 下一篇: h264检测是I帧还是P帧