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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

c ++ 链表_C ++程序查找两个单个链表的并集

發布時間:2025/3/11 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c ++ 链表_C ++程序查找两个单个链表的并集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c ++ 鏈表

Problem statement: Write a C++ program to find the union of two single linked lists.

問題陳述:編寫一個C ++程序來查找兩個單個鏈表的并集。

Example:

例:

Let the first linked list be:5->4->3->6->1->NULLLet the second linked list to be:3->8->9->12->1->NULLSo there union is:5->4->3->6->1->8->9->12->NULL

Solution

Brute force approach:

蠻力法:

One technique can be to include all the nodes of a linked list and for other linked check whether nodes are already appended or not. Such an approach takes O(m*n) times complexity. m, n=length of linked lists.

一種技術可以是包括鏈接列表的所有節點,而對于其他鏈接,請檢查節點是否已附加。 這種方法需要O(m * n)乘以復雜度 。 m , n =鏈表的長度 。

Efficient approach:

高效的方法:

Efficient approach use to use merge sort.

高效的方法用于使用合并排序 。

  • Sort both the linked list using merge sort. ( for detailed refer to: Merge sort for single linked lists)

    使用合并排序對兩個鏈表進行排序。 (有關詳細信息,請參閱: 合并單個鏈接列表的排序 )

  • Scan each linked list and build union according to following:

    掃描每個鏈表并根據以下內容建立并集:

  • IF (list1->data < list2->data)Createnode(list1->data) && append node to the union listTraverse list1 by one step( list1=list1->next) ELSE IF(list1->data ==list2->data)Createnode(list1->data) && append nodeto the union listTraverse list1 by one step( list1=list1->next)Traverse list2 by one step( list2=list2->next) ELSE//list1->data >list2->dataCreatenode(list2->data) && append node to the union listTraverse list2 by one step( list2=list2->next) END IF-ELSE
  • Display the union list

    顯示聯合列表

  • .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    C ++實現查找兩個單個鏈表的并集 (C++ implementation to find Union of two single Linked Lists)

    #include<bits/stdc++.h> using namespace std;class node{public:int data; // data fieldstruct node *next; };void display(class node* head){node* current=head; // current node set to headwhile(current!=NULL){ //traverse until current node isn't NULLprintf("%d ",current->data);current=current->next; // go to next node} }node* creatnode(int d){node* temp=(node*)malloc(sizeof(node));temp->data=d;temp->next=NULL;return temp; } node* mergeList(node* split1,node* split2){node* newhead=NULL;//base casesif(split1==NULL)return split2;if(split2==NULL)return split1;if(split1->data<=split2->data){newhead=split1;newhead->next=mergeList(split1->next,split2);}else{newhead=split2;newhead->next=mergeList(split1,split2->next);}return newhead;}void splitList(node* head,node** split1,node** split2){node* slow=head;node* fast=head->next;while(fast!=NULL){fast=fast->next;if(fast!=NULL){slow=slow->next;fast=fast->next;}}*split1=head;*split2=slow->next;//splitingslow->next=NULL; }void mergeSort(node** refToHead){node* head=*refToHead;node* split1,*split2;//base caseif(head==NULL || head->next==NULL){return;}//split the list in two halvessplitList(head,&split1,&split2);//recursively sort the two halvesmergeSort(&split1);mergeSort(&split2);*refToHead=mergeList(split1,split2);return;}node* findUnion(node* head1, node* head2){if(head1==NULL && head2==NULL)return NULL;node* head3;if(head1->data<head2->data){head3=creatnode(head1->data);head1=head1->next;}else if(head1->data==head2->data){head3=creatnode(head1->data);head1=head1->next;head2=head2->next;}else{head3=creatnode(head2->data);head2=head2->next;}node* temp=head3;while( head1!=NULL && head2!=NULL){if(head1->data<head2->data){temp->next=creatnode(head1->data);temp=temp->next;head1=head1->next;}else if(head1->data==head2->data){temp->next=creatnode(head1->data);temp=temp->next;head1=head1->next;head2=head2->next;}else{temp->next=creatnode(head2->data);temp=temp->next;head2=head2->next;}}while(head1!=NULL){temp->next=creatnode(head1->data);temp=temp->next;head1=head1->next;}while(head2!=NULL){temp->next=creatnode(head2->data);temp=temp->next;head2=head2->next;}return head3;}int main(){printf("creating the linked list by inserting new nodes at the end\n");printf("enter 0 to stop building the list, else enter any integer\n");int k;node* curr,*temp;cout<<"enter list1...\n";scanf("%d",&k);node* head1=creatnode(k); //buliding list, first nodescanf("%d",&k);temp=head1;///inserting at the end//while(k){curr=creatnode(k);temp->next=curr;//appending each nodetemp=temp->next;scanf("%d",&k);}cout<<"displaying list1...\n";display(head1); // displaying the listcout<<"\nenter list2...\n";scanf("%d",&k);node* head2=creatnode(k); //buliding list, first nodescanf("%d",&k);temp=head2;///inserting at the end//while(k){curr=creatnode(k);temp->next=curr;//appending each nodetemp=temp->next;scanf("%d",&k);}cout<<"displaying list1...\n";display(head2);//sort both the listsmergeSort(&head1);mergeSort(&head2);cout<<"\ndisplaying their union...\n";node* head3=findUnion(head1,head2);display(head3);return 0; }

    Output

    輸出量

    creating the linked list by inserting new nodes at the end enter 0 to stop building the list, else enter any integer enter list1... 5 4 3 6 1 0 displaying list1... 5 4 3 6 1 enter list2... 3 8 9 12 1 0 displaying list1... 3 8 9 12 1 displaying their union... 1 3 4 5 6 8 9 12 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    Example with explanation:

    帶有說明的示例:

    First linked list: 5->4->3->6->1->NULL Second linked list:3->8->9->12->1->NULLAfter applying merge sort: First linked list: 1->3->4->5->6->NULL Second linked list: 1->3->8->9->12->NULL ---------------------------------------------------------//for better understanding nodes are represented only by respective values head1=1 head2=1 FindUnion(1,1):0th iteration head1!=NULL&&head2!=NULL head1->data==head2->data //=1 thus head3(head of union list) =1 temp=head3=1 union list up to now: 1->NULL head1=1->next=3; head2=1->next=3;1st iteration head1!=NULL&&head2!=NULL head1->data==head2->data //=3 thus temp->next =3 temp=temp->next=3; union list up to now: 1->3->NULL head1=3->next=4; head2=3->next=8;2nd iteration head1!=NULL&&head2!=NULL head1->data<head2->data //4<8 thus temp->next =head1->data=4 temp=temp->next=4; union list up to now: 1->3->4->NULLhead1=4->next=5;3rd iteration head1!=NULL&&head2!=NULL head1->data<head2->data //5<8 thus temp->next =head1->data=5 temp=temp->next=5; union list up to now: 1->3->4->5->NULL head1=5->next=6;4th iteration head1!=NULL&&head2!=NULL head1->data<head2->data //6<8 thus temp->next =head1->data=6 temp=temp->next=6; union list up to now: 1->3->4->5->6->NULL head1=6->next=NULL;5th iteration head1 ==NULL head2!=NULL thus temp->next =head2->data=8 temp=temp->next=8; union list up to now: 1->3->4->5->6->8->NULL head2=8->next=9;6th iteration head1 ==NULL head2!=NULL thus temp->next =head2->data=9 temp=temp->next=9; union list up to now: 1->3->4->5->6->8->9->NULL head2=9->next=12;7th iteration head1 ==NULL head2!=NULL thus temp->next =head2->data=12 temp=temp->next=12; union list up to now: 1->3->4->5->6->8->9->12->NULLhead2=12->next=NULL;8th iteration: head1==NULL head2==NULL thus no more scanning, union list is builtunion: 1->3->4->5->6->8->9->12->NULL Head3 at 1

    翻譯自: https://www.includehelp.com/cpp-programs/find-union-of-two-single-linked-lists.aspx

    c ++ 鏈表

    總結

    以上是生活随笔為你收集整理的c ++ 链表_C ++程序查找两个单个链表的并集的全部內容,希望文章能夠幫你解決所遇到的問題。

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