兩個有序連結串列序列的交集

Lpy_Now發表於2020-09-27

兩個有序連結串列序列的交集

已知兩個非降序連結串列序列S1與S2,設計函式構造出S1與S2的交集新連結串列S3。

輸入格式:

輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1表示序列的結尾(−1不屬於這個序列)。數字用空格間隔。

輸出格式:

在一行中輸出兩個輸入序列的交集序列,數字間用空格分開,結尾不能有多餘空格;若新連結串列為空,輸出NULL

輸入樣例:

1 2 5 -1
2 4 5 8 10 -1

輸出樣例:

2 5

標程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

//兩題寫法幾乎完全相同

struct Node{                                                       
    int number;                                                  
    Node* next;                                                   
};

inline int read() {                                               
	int number = 0;
	int f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') {
			f = -1;
		}
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		number = number * 10 + ch - '0', ch = getchar();
	}
	return number * f;
}

int main(){
    Node* List1 = (Node*)malloc(sizeof(Node));                      
    List1->next = NULL;                                            
    Node* List2 = (Node*)malloc(sizeof(Node));                     
    List2->next = NULL;                                             
    Node* List3 = (Node*)malloc(sizeof(Node));                     
    List3->next = NULL;                                          
    Node *point = (Node*)malloc(sizeof(Node));                      
    int number = read();                                           
    point = List1;                                                
    while (number != -1) {                                          
        Node *temp = (Node*)malloc(sizeof(Node));                   
        temp->number = number;                                    
        temp->next = NULL;
        point->next = temp;                                        
        //point = temp;                                            
        memcpy(&point, &temp, sizeof(temp));
        number = read();                                           
    }
    point = List2;                                                 
    number = read();
    while (number != -1) {
        Node *temp = (Node*)malloc(sizeof(Node));
        temp->number = number;
        temp->next = NULL;
        point->next = temp;
        point = temp;
        number = read();
    }
    Node *point1, *point2, *point3;                                 
    point3 = List3;
    point1 = List1->next, point2 = List2->next;
    while (point1 != NULL && point2 != NULL) {                     
        if (point1->number == point2->number) {
            point3->next = point1;
            point3 = point1;
            point1 = point1->next;
            point2 = point2->next;
        }
        else if (point1->number < point2->number) {
            point1 = point1->next;
        }
        else {
            point2 = point2->next;
        }
    }
    point = List3->next; 
    if (point == NULL) {                                          
        printf("NULL");
    }
    else {                                                        
        bool flag = false;
        while (point != NULL) {
            if (flag == false) {
                flag = !flag;
            }
            else {
                printf(" ");
            }
            printf("%d", point->number);
            point = point->next;
        }
    }
    return 0;
}

相關文章