总是答案错误,但测试数据都过了
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef int elemType;
typedef struct LNode
{
elemType data;
struct LNode* next;
}LNode;
LNode *head,*p;
LNode* initList()
{
head=(LNode*)malloc(sizeof(LNode));
if(!head)
{
printf("memory allocate error");
exit(1);
}
head->next=NULL;
return head;
}
void print(LNode *head)
{
struct LNode*p;
p=head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
void shanchu(LNode* head)
{
LNode* p=head->next,*q,*r,*t;
while(p)
{
q=p;
r=q->next;
while(r)
{
if(r->data==p->data)
{
t=r->next;
q->next=t;
free(r);
r=t;
}
else
{
q=r;
r=r->next;
}
}
p=p->next;
}
}
void interset(LNode*ha,LNode*hb)
{
LNode *pa=ha->next,*pb,*s,*r,*hc;
r=hc=(LNode*)malloc(sizeof(LNode));
while(pa)
{
pb=hb->next;
while(pb!=NULL&&pb->data!=pa->data)
pb=pb->next;
if(pb!=NULL)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=pb->data;
r->next=s;
r=s;
}
pa=pa->next;
}
r->next=NULL;
shanchu(hc);
print(hc);
}
void destoryList(LNode* head)
{
struct LNode*p;
p=head;
while(p)
{
head=head->next;
free(p);
p=head;
}
}
int main()
{
LNode *ha,*hb,*ra,*rb,*p,*q,*hc;
int i=0,sum=0,num=0,count=0;
int a[500];
char str1[500],str2[500];
ha=ra=initList();
hb=rb=initList();
gets(str1);
gets(str2);
while(str1[i]!='\0')
{
if(str1[i]>='0'&&str1[i]<='9')
{
sum=sum*10+((int)str1[i]-48);
a[count]=sum;
num=0;
}
else
{
num=num+1;
if(num==1)
count+=1;
{
sum=0;
}
}
i++;
}
for(i=0;i<count+1;i++)
{
p=(LNode*)malloc(sizeof(LNode));
p->data=a[i];
ra->next=p;
ra=p;
}
ra->next=NULL;
i=0;sum=0;num=0;count=0;
while(str2[i]!='\0')
{
if(str2[i]>='0'&&str2[i]<='9')
{
sum=sum*10+((int)str2[i]-48);
a[count]=sum;
num=0;
}
else
{
num=num+1;
if(num==1)
count+=1;
{
sum=0;
}
}
i++;
}
for(i=0;i<count+1;i++)
{
q=(LNode*)malloc(sizeof(LNode));
q->data=a[i];
rb->next=q;
rb=q;
}
rb->next=NULL;
interset(ha,hb);
destoryList(ha);
destoryList(hb);
return 0;
}
Description
(线性表)己知两个线性表A ,B均以带头结点的单链表作存储结构,且表中元素按值递增有序排列。设计算法求出A与B的交集C,要求C另开辟存储空间,要求C同样以元素值的递增序的单链表形式存贮。
Input
1 2 3 4 5 6
2 3 4 5 6
Output
2 3 4 5 6
Sample Input
11 22 33 44 55 66
22 22 33 56
Sample Output
22 33
题目网址是:http://acm.zjgsu.






