一道简单c程序,不知道哪错了,求大神相助~

Iantone 2012-10-07 09:41:14
题目:两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,编写算法将表A和B归并成一个按元素非递减有序排列的线性表C,并要求利用原表(A和B)的节点存放C。
下面是我写的代码,就是不知道哪错了~
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct list)
int s;
struct list
{
int data;
struct list *next;
};
list* create(list a)
{

list *head=&a;
list *p=head->next;
int i=1,d;

printf("链表长度:");
scanf("%d",&s);

for(;i<=s;i++)
{
scanf("%d",&d);
p->data=d;
p->next=(list *)malloc(LEN);
p=p->next;
}
return head;
}
list* compare(list *heada,list *headb,int s1,int s2)
{
list *p1=heada->next,*p2=headb->next,*p3,*headc;
int i=1,j=1;
p3=heada<=headb?heada:headb;
headc=p3;
p3=p3->next;
while(i>s1||j>j)
{
if(p1->data<=p2->data)
{
p3->next=p1;
i++;
}
else
{
p3->next=p2;
j++;
}
}
if(i>s1)
p3->next=p2;
else
p3->next=p1;
return headc;
}
void display(list *headc,int s3)
{
int i=0;
list *p=headc->next;
for(;i<=s3;i++)
{
printf("%d ",*p);
p=p->next;
}
}
void main()
{
int s1,s2,s3;
list *heada,*headb,*headc;
list a,b,c;
heada=create(a);
s1=s;
headb=create(b);
s2=s;
s3=s1+s2;
headc=compare(heada,headb,s1,s2);
display(headc,s3);
}
...全文
248 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhcosin 2012-10-09
  • 打赏
  • 举报
回复
楼主,给你一些建议:
1.要有调试排错的意识。
2.详细你遇到的问题,而不是一句“有错,请帮我查错”这样的空话。
wumn29 2012-10-09
  • 打赏
  • 举报
回复
list* create(list a)
{

list *head=&a;//这里a是存在栈上, 若真需要传参,那么参数应该传list*


newtee 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 1 楼 的回复:
C/C++ code

typedef struct node
{
int data;
struct node *next;
}list;
list* create()
{
int i;
list *s,*p,*head;;
head=(list *)malloc(LEN);
head->next=NULL;
p=head
printf("……
[/Quote]没错 但是你函数中返回的是list * 我帮你改了一下
Iantone 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
C/C++ code

typedef struct node
{
int data;
struct node *next;
}list;
list* create()
{
int i;
list *s,*p,*head;;
head=(list *)malloc(LEN);
head->next=NULL;
p=head
printf("链表长度:");
scan……
[/Quote]
struct list
{
int data;
struct list *next;
};
我这部分没错吧?
青芒Only 2012-10-09
  • 打赏
  • 举报
回复
创建链表都是要传递链表指针进入函数里,要么就在里面malloc返回指针
newtee 2012-10-07
  • 打赏
  • 举报
回复
建议先把书本上创建单链表 单链表的插入再看看 你的代码一大堆错误
newtee 2012-10-07
  • 打赏
  • 举报
回复
typedef struct node
{
int data;
struct node *next;
}list;
list* create()
{
int i,len;
list *s,*p,*head;;
head=(list *)malloc(LEN);
head->next=NULL;
p=head
printf("链表长度:");
scanf("%d",&len);
for(i=1;i<=s;i++)
{
s=(list *)malloc(LEN);
scanf("%d",&s->data);
p->next=s;
p=s;
}
newtee 2012-10-07
  • 打赏
  • 举报
回复
typedef struct node
{
int data;
struct node *next;
}list;
list* create()
{
int i;
list *s,*p,*head;;
head=(list *)malloc(LEN);
head->next=NULL;
p=head
printf("链表长度:");
scanf("%d",&s);
for(i=1;i<=s;i++)
{
s=(list *)malloc(LEN);
scanf("%d",&s->data);
p->next=s;
p=s;
}
return head;
}
链表都创建错了

70,039

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧