Programming Ability Test学习 2-12. 两个有序链表序列的交集(20)

2-12. 两个有序链表序列的交集(20)

时间限制
400 ms
内存限制
64000 kB
代码长度限制
8000 B
判题程序
Standard

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。

输入格式说明:

输入分2行,分别在每行给出由若干个正整数构成的非降序序列,用-1表示序列的结尾(-1不属于这个序列)。数字用空格间隔。

输出格式说明:

在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出“NULL”。

样例输入与输出:

序号 输入 输出
1
1 2 5 -1
2 4 5 8 10 -1
2 5
2
1 3 5 -1
2 4 6 8 10 -1
NULL
3
1 2 3 4 5 -1
1 2 3 4 5 -1
1 2 3 4 5
4
3 5 7 -1
2 3 4 5 6 7 8 -1
3 5 7
5
-1
10 100 1000 -1
NULL

 

 

 

 

#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
typedef struct Node
{
   int dex;
   //int cof;
   struct Node * Next;
}node;


int main()
{
    //Qlink newLink;
    //newLink=(Link*)malloc(sizeof(Link));
    struct Node *head,*tail;
    head=(node*)malloc(sizeof(node));
    tail=(node*)malloc(sizeof(node));
    tail->Next=NULL;
    head->Next=tail;

    struct Node *pthis,*pthat;
    pthis=head;pthat=pthis;
    //第一个链表 
    int dex;
    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;
    }
    //第二个链表 
    struct Node *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;
    }
    
    
    //遍历 
    pthat=head->Next;
    pthis=head1->Next;
    
    
    
    //交集链表 
        struct Node *head2,*tail2;
        head2=(node*)malloc(sizeof(node));
        tail2=(node*)malloc(sizeof(node));
        struct Node *newNode,*pnew;
        
        tail2->Next=NULL;
        head2->Next=tail2;
        newNode=head2;pnew=newNode;
            
        

     if(pthat->dex==-1||pthis->dex==-1)//存在空链表 
       printf("NULL\n");
     else
     {
        
        while(pthis->dex!=-1&&pthat->dex!=-1)
        {
            if(pthis->dex==pthat->dex)//比较每一位 
            {
               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;
            else pthat=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");
            else printf(" ");
            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

 

posted @ 2015-08-20 16:35  GGRoddick  阅读(405)  评论(0编辑  收藏  举报