7-3 两个有序链表序列的交集 (20 分)
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL
。
输入样例:
1 2 5 -1
2 4 5 8 10 -1
输出样例:
2 5
方法一:
#include <stdio.h>
#include <stdlib.h>
struct List {
int sum;
struct List* next;
};
struct List* in();
struct List* occur(struct List* h1,struct List* h2);
int main() {
struct List *head1,*head2,*head3;
head1=head2=head3=NULL;
head1=in();
head2=in();
head3=occur(head1,head2);
if(head3==NULL)
printf("NULL");
while(head3!=NULL) {
printf("%d",head3->sum);
if(head3->next!=NULL)
printf(" ");
head3=head3->next;
}
return 0;
}
struct List* in() {
struct List* head,*tran;
head=tran=NULL;
int sum;
scanf("%d",&sum);
while(sum!=-1) {
struct List* p=(struct List*)malloc(sizeof(struct List));
p->sum=sum;
p->next=NULL;
if(head==NULL) {
head=p;
tran=p;
} else {
tran->next=p;
tran=p;
}
scanf("%d",&sum);
}
return head;
}
struct List* occur(struct List* h1,struct List* h2) {
struct List *h3,*tran3,*tran;
tran=h2;
h3=tran3=NULL;
if(h1==NULL||h2==NULL)
return NULL;
while(h1!=NULL) {
tran=h2;
while(tran!=NULL) {
if(tran->sum==h1->sum)
break;
tran=tran->next;
}
if(tran!=NULL) {
struct List* p=(struct List*)malloc(sizeof(struct List));
p->sum=h1->sum;
p->next=NULL;
if(h3==NULL) {
h3=p;
tran3=p;
} else {
tran3->next=p;
tran3=p;
}
}
h1=h1->next;
}
return h3;
}
//fist:考虑情况不周到
//second :我对指针的定义不清楚以及将各个数据搞混
//暴力破解
方法二:
#include <stdio.h>
#include <stdlib.h>
#define max 10000000
typedef struct List {
int sum[max];
int last;
}shu;
shu *begin();
void input(shu *head);
shu *occur(shu *h1,shu *h2);
int erfengfa(int c,int left,int right,shu *b);
int main() {
shu *list1,*list2,*list3;
list1=begin();
list2=begin();
input(list1);
input(list2);
list3=occur(list1,list2);
if(list3==NULL)
printf("NULL");
else{
for(int i=0;i<=list3->last;i++){
printf("%d",list3->sum[i]);
if(i!=list3->last)
printf(" ");
}
}
return 0;
}
//初始化
shu* begin() {
shu *head;
head=(shu*)malloc(sizeof(shu));
head->last=-1;
return head;
}
//输入
void input(shu *head) {
int x,k=0;
scanf("%d",&x);
while(x!=-1) {
head->sum[k]=x;
k++;
head->last++;
scanf("%d",&x);
}
}
//第一功能
shu *occur(shu *h1,shu *h2) {
int a,k=0;
shu *h3=(shu*)malloc(sizeof(shu));
h3->last=-1;
for(int i=0; i<=h1->last; i++) {
//printf("%d",h2->last);
a=erfengfa(h1->sum[i],0,h2->last,h2);/*找到返回该值,找不到返回-1;*/
if(a>=0){
h3->sum[k]=a;
k++;
h3->last++;
}
}
if(h3->last>=0)
return h3;
else
return NULL;
}
//error
//二分法:主要算法
int erfengfa(int c,int left,int right,shu *b){
int a=0;
int might=(left+right)/2;
if(b->sum[might]==c)
return c;
if(right<left)
return -1;
if(c>b->sum[might]){
a=erfengfa(c,might+1,right,b);
}else if(c<b->sum[might]){
a=erfengfa(c,left,might-1,b);
}
if(a>=0)
return c;
if(a<0)
return -1;
}
方法三:(本人自己的方法)
//考虑情况不周到
#include <stdio.h>
#include <stdlib.h>
struct List {
int sum;
struct List* next;
};
struct List* in();
struct List* occur(struct List* h1,struct List* h2);
int main() {
struct List *head1,*head2,*head3;
head1=head2=head3=NULL;
head1=in();
head2=in();
head3=occur(head1,head2);
if(head3==NULL)
printf("NULL");
while(head3!=NULL) {
printf("%d",head3->sum);
if(head3->next!=NULL)
printf(" ");
head3=head3->next;
}
return 0;
}
struct List* in() {
struct List* head,*tran;
head=tran=NULL;
int sum;
scanf("%d",&sum);
while(sum!=-1) {
struct List* p=(struct List*)malloc(sizeof(struct List));
p->sum=sum;
p->next=NULL;
if(head==NULL) {
head=p;
tran=p;
} else {
tran->next=p;
tran=p;
}
scanf("%d",&sum);
}
return head;
}
struct List* occur(struct List* h1,struct List* h2) {
struct List *h3,*tran3,*tran;
tran=h2;
h3=tran3=NULL;
if(h1==NULL||h2==NULL)
return NULL;
//小的移动,相等一起移动,
while(h1!=NULL&&h2!=NULL) {
if(h1->sum==h2->sum) {
struct List* p=(struct List*)malloc(sizeof(struct List));
p->sum=h1->sum;
p->next=NULL;
if(h3==NULL) {
h3=p;
tran3=p;
} else {
tran3->next=p;
tran3=p;
}
h1=h1->next;
h2=h2->next;
continue;
}
if(h1->sum>h2->sum){
h2=h2->next;
continue;
}
if(h1->sum<h2->sum){
h1=h1->next;
continue;
}
}
return h3;
}
网上有一个人比我的好一点
#include <stdlib.h>
#include<stdio.h>
struct store{
int date;
struct store *Next;
};
typedef struct store *List;
List SetUp()
{
List L=(List)malloc(sizeof(struct store));
L->date=0;L->Next=NULL;
return L;
}
List charu(List L,int a)
{
List l=(List)malloc(sizeof(struct store));
l->date=a;l->Next=L->Next;
L->Next=l;
return L;
}
List chuangzao(List L)
{
int a=0;
scanf("%d",&a);
while(a!=-1)
{
charu(L,a);
scanf("%d",&a);
}
return L;
}
void shuchu(List L)
{
int i=1;
List l=L->Next;
if(l==NULL)
printf("NULL");
while(l!=NULL)
{
if(i)
{
i=0;
printf("%d",l->date);
}
else
printf(" %d",l->date);
l=l->Next;
}
}
List jiaoji(List L1,List L2,List L3)
{
List l1=L1->Next,l2=L2->Next;
while(l1!=NULL)
{
while(l2!=NULL)
{
if(l2->date==l1->date)
{
charu(L3,l2->date);
l2=l2->Next;
break;
}
if(l2->date < l1->date)
{
break;
}
l2=l2->Next;
}
l1=l1->Next;
}
}
int main()
{
List L1=SetUp(),L2=SetUp(),L3=SetUp();
chuangzao(L1);
chuangzao(L2);
jiaoji(L1,L2,L3);
shuchu(L3);
return 0;
}