C语言程序求纠错!!~在线等!……

题目:
已知两个元素按照值递增有序排列的线性表A和B,且同一表中元素值各不相同。试构造一个线性表C,其元素为A和B中元素的交集,且元素按照值递增有序排列。(采用单向链表的存储方式)
下面是我写的程序:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
node *input()
{
int i;
node *p,*q,*r;
q=(node *)malloc(sizeof(node));
q->next=NULL;
p=q;
printf("请输入不重复的数据(递增,以0结尾):\n");
scanf("%d",&i);
while(i!=0)
{
r=(node *)malloc(sizeof(node));
r->data=i;
r->next=NULL;
p->next=r;
p=r;
scanf("%d",&i);
}
return(q);
}
void output(node *head)
{
node *p;
p=head;
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}
void fun(node *ahead,node *bhead,node *chead)
{
node *p,*q,*r;
p=ahead->next;
q=bhead->next;
r=chead->next;
while(p!=NULL&&q!=NULL)
{
if(p->data<q->data) p=p->next;
else {if(p->data==q->data) {r->data=p->data;p=p->next;q=q->next;r=r->next;}
else q=q->next;}}
}
main()
{
node *ahead,*bhead,*chead;
printf("线性表A\n");
ahead=input();
printf("线性表B\n");
bhead=input();
fun(ahead,bhead,chead);
printf("取交集后的数据:\n");
output(chead);
}
调试时一执行到fun函数,就错误了。。。
求大神帮帮忙呀!!!感激不尽。。。
#include <stdio.h>败信
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;

node *input()
{
int i;
node *p,*q,*r;
q=(node *)malloc(sizeof(node));
q->next=NULL;
p=q;
printf("请输入不重复的数据(递增码携,以0结尾):\n");
scanf("%d",&i);
while(i!=0)
{
r=(node *)malloc(sizeof(node));
r->data=i;
r->next=NULL;
p->next=r;
p=r;
scanf("%d",&i);
fflush(stdin);
}
return(q);
}

void output(node *head)
{
node *p;
p=head->next;
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}

void fun(node *ahead,node *bhead,node *chead)
{
node *p,*q,*r,*t;
p=ahead->next;
q=bhead->迟枯伏next;
r=chead;
while(p!=NULL&&q!=NULL)
{
if(p->data<q->data) p=p->next;
else
{
if(p->data==q->data)
{
t=(node *)malloc(sizeof(node));
t->next=NULL;
t->data=p->data;
r->next=t;
p=p->next;
q=q->next;
r=r->next;
}
else
q=q->next;
}
}
}

node * initialize(node *head)
{
head=(node *)malloc(sizeof(node));
head->next=NULL;
return head;
}
void main()
{
node *ahead,*bhead,*chead=NULL;
printf("线性表A\n");
ahead=input();
printf("线性表B\n");
bhead=input();
chead=initialize(chead);
fun(ahead,bhead,chead);
printf("取交集后的数据:\n");
output(chead);
}

增加一个初始化chead的函数,另外你的fun函数中涉及c线性表的有逻辑错误。
你的程序有点乱,改起来很麻咐迹穗州春烦,只好试一下这个
给你
#include<stdio.h>
#include<malloc.h>
#include<iostream>
typedef struct
{
int *num;
int len;
}list;
void display(list l)
{
int k=0;
int *p=l.num;
for(;k<l.len;k++)
printf("%d",*p++);
printf("\n");
}
int creat(list <,int i)
{
if(!i)return0;
lt.num = (int*)malloc(i*sizeof(int));
if(!lt.num)return0;
lt.len=i;
int*p=lt.num;
for(int k =0;k<i;k++)
{
scanf("%d",p++);
}
return 1;
}
int merlist(list la,list lb,list &lc)
{
int *pa =la.num,*pb=lb.num,*pc;
lc.num = (int*)malloc((la.len+lb.len)*sizeof(int));
if(!lc.num)return0;
int*pa_len=la.len+la.num;
int*pb_len=lb.len+lb.num;
pc=lc.num;
for(;pa<=pa_len;pa++)
{
for(pb=lb.num;pb<=pb_len;pb++)
{
if(*pa==*pb)
{
for(int*p=pb;p<衡卜=pb_len;p++)*p=*(p+1);
pb_len--;
break;
}
}
if(pb>pb_len)*pc++=*pa,lc.len+=1;
}
if(pb_len)
{ --pc;
for(pb=lb.num;pb<=pb_len;)
{
*pc=*pb++;lc.len++;pc++;
}
--pc;--lc.len;
}
if(!lc.len)
{lc.num=la.num;
lc.len=la.len;}
return 1;
}
int main()
{
int i;
listla,lb,lc;
lc.len=0;
scanf("%d",&i);
if(creat(la,i))
{
scanf("%d",&i);
if(creat(lb,i))
{
printf("listla :");display(la);
printf("listlb :");display(lb);
merlist(la,lb,lc);
printf("listlc :");display(lc);
}
else
{
lc.num =la.num;lc.len=la.len;
printf("listla :");display(la);
printf("listlc :");display(lc);
}

}
else
{
scanf("%d",&i);
if(creat(lb,i))
{
lc.num= lb.num;
lc.len=lb.len;
printf("listlb :");display(lb);
printf("listlc :");display(lc);
}
elsereturn 0;
}
return 0;}

 修改后的程序如下(修改地方已用符号标出):

       #include <stdio.h>

  #include <stdlib.h>

  #include <string.h>

  typedef struct Node

  {

  int data;

  struct Node *next;

  }node;

  node *input()

  {

  int i;

  枝链敬node *p,*q,*r;

  q=(node *)malloc(sizeof(node));

  q->next=NULL;

  p=q;

  printf("请输入不重复的数据(递增,以0结尾):\n");

  scanf("%d",&i);

  while(i!=0)

  {

  r=(node *)malloc(sizeof(node));

  r->data=i;

  r->next=NULL;

  p->next=r;

  p=r;

  猛慎scanf("%d",&i);

  }

  return(q);

  }

  void output(node *head)

  {

  node *p;

  p=head->next;

  while(p)

  {

  printf("%d\n",p->data);

  p=p->next;

  }

  }

  • void fun(node *ahead,node *bhead,node * &chead)      //chead对唤升其引用

  {

  node *p,*q,*r,*tmp;

  p=ahead->next;

  q=bhead->next;

  • chead=(node*)malloc(sizeof(node));

  chead->next=NULL;

  r=chead;

  while(p!=NULL&&q!=NULL)

  {

  if(p->data<q->data) p=p->next;

  else

  {

  if(p->data==q->data)

  {

  • tmp=(node*)malloc(sizeof(node));

  • tmp->data=p->data;

  • tmp->next=NULL;

  • p=p->next;

  •        q=q->next;

  • r->next=tmp;

  • r=r->next;

  }

  else q=q->next;

  }

  }

  }

  int main()

  {

  node *ahead,*bhead,*chead;

  printf("线性表A\n");

  ahead=input();

  printf("线性表B\n");

  bhead=input();

  fun(ahead,bhead,chead);

  printf("取交集后的数据:\n");

  output(chead);

  return 0;

  }