两个链表求交集_实现两个排序链表的并集和交集
兩個鏈表求交集
In computer science, a linked list is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. In its most basic form, each node contains data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. More complex variants add additional links, allowing more efficient insertion or removal of nodes at arbitrary positions.
在計算機科學中,鏈表是數據元素的線性集合,其順序不是由它們在內存中的物理位置給出的。 相反,每個元素都指向下一個。 它是一種數據結構,由節點的集合組成,這些節點一起代表一個序列。 以其最基本的形式,每個節點都包含數據和對序列中下一個節點的引用(換句話說,就是鏈接)。 這種結構允許在迭代過程中從序列中的任何位置有效插入或刪除元素。 更復雜的變體會添加其他鏈接,從而可以更有效地在任意位置插入或刪除節點。
Union of two linked lists can be found by using merging the lists in a sorted manner.
兩個鏈接列表的聯盟可以通過合并列表的排序方式找到。
The intersection of the two lists can be found by only taking common elements while merging the two lists.
通過合并兩個列表時僅采用公共元素,可以找到兩個列表的交集 。
It has been assumed that the linked lists are sorted.
假定已對鏈表進行排序 。
Algorithm:
算法:
Union(L1,L2)
聯合(L1,L2)
1) Declare node pointer output, output Tail as NULL2) Repeat steps 3 to 9 while L1!=NULL AND L2!=NULL3) Make a newNode and set its next = NULL4) If L1->data < L2->data thenSet newNode->data = L1->dataSet L1 = L1->next5) Else if L1->data > L2->data thenSet newNode->data = L2->dataL2 = L2->next6) Elsei) Set Data = L1->dataii) Set newNode->data = Dataiii) Repeat steps a) and b) while L1!=NULL AND L2!=NULL AND L1->data == Data AND L2->data == Dataa) Set L1 = L1->nextb) Set L2 = L2->next7) If output == NULL thenSet Output = outputTail = newNode8) Elsea) Set outputTail->next = newNodeb) Set outputTail = outputTail->next9) Repeat steps 10 to 14 while L1!=NULL10) Make a newNode11) Set outputTail->next = newNode12) Set outputTail = outputTail->next13) Set outputTail->data = L1->data14) Set L1 = L1->nextRepeat steps 15 to 19 while L2!=NULL15) Make a newNode16) Set outputTail->next = newNode17) Set outputTail = outputTail->next18) Set outputTail->data = L2->data19) Set L2 = L2->nextReturn outputIntersection(L1,L2)
交叉路口(L1,L2)
1. If L1 or L2 is NULL then return NULL2. Declare node pointers output, outputTail as null.3. Repeat steps 4 to 6 while L1!=NULL AND L2!=NULL4. If L1->datadata thenSet L1 = L1->next5. Else If L2->datadata thenSet L2 = L2->next6. Elsea) Declare and set data = L1->datab) Make a newNodec) Set newNode->data = data and newNode->next = NULLd) If output == null theni. Set output = outputTail = newNodee) Elsei. Set outputTail->next = newNodeii. Set outputTail = outputTail->nextf) Repeat steps i and ii while L1!=NULL AND L2!=NULL AND L1->data == data AND L2->data == datai. Set L1 = L1->nextii. Set L2 = L2->next.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Code:
碼:
#include <stdio.h> #include <stdlib.h>struct node{struct node*next;int data; };struct node * Union(struct node * L1, struct node * L2){struct node * output = NULL;struct node * outTail = NULL;while(L1&&L2){struct node * newNode = (struct node *) malloc(sizeof(struct node));newNode->next = NULL;if(L1->data<L2->data){newNode->data = L1->data;L1 = L1->next;}else if(L1->data>L2->data){newNode->data = L2->data;L2 = L2->next;}else{int data = L1->data;newNode->data = data;while(L1 && L2 && L1->data == data && L2->data == data){L1 = L1->next;L2 = L2->next;}}if(!output)output = outTail = newNode;else{outTail->next = newNode;outTail = outTail->next;}}while(L1){outTail->next = (struct node *) malloc(sizeof(struct node));outTail = outTail->next;outTail->data = L1->data;L1 = L1->next;}while(L2){outTail->next = (struct node *) malloc(sizeof(struct node));outTail = outTail->next;outTail->data = L2->data;L2 = L2->next;}outTail->next = NULL;return output; }struct node * intersection(struct node * L1, struct node* L2){if(L1 == NULL || L2 == NULL)return NULL;struct node * output = NULL;struct node * outTail = NULL;while(L1&&L2){if(L1->data<L2->data){L1 = L1->next;}else if(L2->data<L1->data){L2 = L2->next;}else{int data = L1->data;struct node * newNode = (struct node *) malloc(sizeof(struct node));newNode->data = data;newNode->next = NULL;if(output == NULL){outTail = output = newNode;}else{outTail->next = newNode;outTail = outTail->next;}while(L1 && L2 && L1->data == data && L2->data == data){L1 = L1->next;L2 = L2->next;}}}return output; }struct node * createList(int listNum){struct node * list = NULL;struct node * list_tail = NULL;printf("Enter elements of List %d in increasing order\n",listNum);char ch = 'y';do{int data;printf("Enter element : ");scanf("%d",&data);struct node * newNode = (struct node *) malloc(sizeof(struct node));newNode->data = data;newNode->next = NULL;if(list == NULL){list = list_tail = newNode;}else{list_tail->next = newNode;list_tail = list_tail->next;}printf("Would you like to insert another element [Y/N] : ");scanf(" %c",&ch);}while(ch == 'y' || ch == 'Y');return list; }void print(struct node * list){if(list == NULL){printf("Empty List\n");return;}while(list!=NULL){printf("%d ",list->data);list = list->next;}printf("\n"); }int main() {struct node * L1 = NULL;struct node * L2 = NULL;struct node * L3 = NULL;struct node * L4 = NULL;L1 = createList(1);L2 = createList(2);printf("List 1 : ");print(L1);printf("List 2 : ");print(L2);printf("Union : ");L3 = Union(L1, L2);print(L3);printf("Intersection : ");L4 = intersection(L1, L2);print(L4);return 0; }Output
輸出量
Enter elements of List 1 in increasing order Enter element : 1 Would you like to insert another element [Y/N] : Y Enter element : 4 Would you like to insert another element [Y/N] : Y Enter element : 9 Would you like to insert another element [Y/N] : Y Enter element : 27 Would you like to insert another element [Y/N] : N Enter elements of List 2 in increasing order Enter element : 4 Would you like to insert another element [Y/N] : Y Enter element : 9 Would you like to insert another element [Y/N] : Y Enter element : 18 Would you like to insert another element [Y/N] : Y Enter element : 22 Would you like to insert another element [Y/N] : Y Enter element : 30 Would you like to insert another element [Y/N] : N List 1 : 1 4 9 27 List 2 : 4 9 18 22 30 Union : 1 4 9 18 22 27 30 Intersection : 4 9翻譯自: https://www.includehelp.com/data-structure-tutorial/implement-union-and-intersection-of-two-sorted-linked-lists.aspx
兩個鏈表求交集
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的两个链表求交集_实现两个排序链表的并集和交集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在React Native中使用文本
- 下一篇: dfs文件服务器访问权限,fastDFS