Programming Ability Test Study 2-12. Intersection of two sequential list sequences (20)

Source: Internet
Author: User

2-12. Intersection of two ordered list sequences (20) time limit MS Memory limit 64000 KB code length limit 8000 B procedure Standard

It is known that two non-descending list sequences S1 and S2, the design function constructs the intersection of S1 and S2 new linked list S3.

Input Format Description:

Input is divided into 2 lines, respectively, in each row given by a number of positive integers consisting of a non-descending sequence, with 1 to indicate the end of the sequence (1 does not belong to this sequence). The number is separated by a space.

Output Format Description:

In a row, output a sequence of two input sequences, separated by a space between the numbers, with no extra spaces at the end, or null if the new list is empty.

Sample input and output:

Serial number Input Output
1
1 2 5-12 4 5 8 10-1
2 5
2
1 3 5-12 4 6 8 10-1
Null
3
1 2 3 4 5-11 2 3 4 5-1
1 2 3) 4 5
4
3 5 7-12 3 4 5 6 7 8-1
3 5 7
5
-110 100 1000-1
Null

#include <stdio.h>#include<stdlib.h>#include<malloc.h>typedefstructnode{intDex; //int cof;   structNode *Next;} node;intMain () {//QLink NewLink; //newlink= (link*) malloc (sizeof (Link ));    structNode *head,*tail; Head= (node*)malloc(sizeof(node)); Tail= (node*)malloc(sizeof(node)); Tail->next=NULL; Head->next=tail; structNode *pthis,*Pthat; Pthis=head;pthat=pthis; //First linked list    intDex;  while(SCANF ("%d",&Dex)) {pthis= (node*)malloc(sizeof(node)); Pthis->dex=Dex; Pthis->next=pthat->Next; Pthat->next=pthis; Pthat=pthis; if(dex==-1) Break; }    //second list of links    structNode *head1,*Tail1; Head1= (node*)malloc(sizeof(node)); Tail1= (node*)malloc(sizeof(node)); Tail1->next=NULL; Head1->next=Tail1; Pthis=head1;pthat=pthis;    GetChar ();  while(SCANF ("%d",&Dex)) {pthis= (node*)malloc(sizeof(node)); Pthis->dex=Dex; Pthis->next=pthat->Next; Pthat->next=pthis; Pthat=pthis; if(dex==-1) Break; }            //TraversePthat=head->Next; Pthis=head1->Next; //intersection linked list        structNode *head2,*Tail2; Head2= (node*)malloc(sizeof(node)); Tail2= (node*)malloc(sizeof(node)); structNode *newnode,*pnew; Tail2->next=NULL; Head2->next=Tail2; NewNode=head2;pnew=NewNode; if(pthat->dex==-1|| pthis->dex==-1)//existence of empty linked listprintf"null\n"); Else     {                 while(pthis->dex!=-1&&pthat->dex!=-1)        {            if(Pthis->dex==pthat->dex)//compare each bit{NewNode= (node*)malloc(sizeof(node)); NewNode->dex=pthat->Dex; NewNode->next=pnew->Next; Pnew->next=NewNode; Pnew=NewNode; Pthis=pthis->Next; Pthat=pthat->Next; }             Else if(Pthis->dex<pthat->dex) pthis=pthis->Next; ElsePthat=pthat->Next; } pthat=head2->Next; if(pthat->next==null) printf ("null\n"); Else{         while(pthat->next!=NULL) {printf ("%d",pthat->Dex); if(pthat->next->next==null) printf ("\ n"); Elseprintf" "); Pthat=pthat->Next; }        }     }        //Free (pthat);     Free(pthis); Free(head); Free(tail); Free(HEAD1);  Free(TAIL1); Free(HEAD2); Free(TAIL2); Free(NewNode); return 0;} 
View Code

Programming Ability Test Study 2-12. Intersection of two sequential list sequences (20)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.