各位朋友能不能进来帮忙看一下小弟我的这个程序哪里有有关问题
各位朋友能不能进来帮忙看一下我的这个程序哪里有问题?
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct node
{
int data;
struct node *next;
}LNode,*LinkList;
LinkList create_LinkList()
{
int x;
LinkList L;
LNode *r;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
r=L;
printf("input the value x:\n");
scanf("%d",&x);
while(x)
{
r->next=(LNode *)malloc(sizeof(LNode));
r->next->data=x;
r=r->next;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
LinkList In_LinkList(LinkList A,LinkList B)
{
LinkList C;
LNode *p,*q,*m;
C=(LinkList)malloc(sizeof(LNode));
C->next=NULL;
m=C;
p=A->next;
q=B->next;
while(p&&q)
{
if(p->data==q->data)
{
m->next=p;
m=p;
m->next=NULL;
p=p->next;
q=q->next;
}
else
q=q->next;
}
return C;
}
void Print_LinkList(LinkList p)
{
LNode *q=p->next;
while(q)
{
printf("%3d",q->data);
q=q->next;
}
printf("\n");
}
void main()
{
LinkList A,B,C;
C->next=NULL;
A=create_LinkList();
B=create_LinkList();
C=In_LinkList(A,B);
printf("A: ");
Print_LinkList(A);
printf("\nB: ");
Print_LinkList(B);
printf("\nC: ");
Print_LinkList(C);
getch();
}
题目是:两个递增单链表A、B,取另一个单链表C,使其为A、B的交集,且仍保持递增。。
运行后,C总是显示不了,改了很多次也不行。。
------解决方案--------------------
这种问题,要自己学会设置断点,逐步调试,看哪里出了问题,这是程序员必须会的
------解决方案--------------------
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct node
{
int data;
struct node *next;
}LNode,*LinkList;
LinkList create_LinkList()
{
int x;
LinkList L;
LNode *r;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
r=L;
printf("input the value x:\n");
scanf("%d",&x);
while(x)
{
r->next=(LNode *)malloc(sizeof(LNode));
r->next->data=x;
r=r->next;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
LinkList In_LinkList(LinkList A,LinkList B)
{
LinkList C;
LNode *p,*q,*m;
C=(LinkList)malloc(sizeof(LNode));
C->next=NULL;
m=C;
p=A->next;
q=B->next;
while(p&&q)
{
if(p->data==q->data)
{
m->next=p;
m=p;
m->next=NULL;
p=p->next;
q=q->next;
}
else
q=q->next;
}
return C;
}
void Print_LinkList(LinkList p)
{
LNode *q=p->next;
while(q)
{
printf("%3d",q->data);
q=q->next;
}
printf("\n");
}
void main()
{
LinkList A,B,C;
C->next=NULL;
A=create_LinkList();
B=create_LinkList();
C=In_LinkList(A,B);
printf("A: ");
Print_LinkList(A);
printf("\nB: ");
Print_LinkList(B);
printf("\nC: ");
Print_LinkList(C);
getch();
}
题目是:两个递增单链表A、B,取另一个单链表C,使其为A、B的交集,且仍保持递增。。
运行后,C总是显示不了,改了很多次也不行。。
C语言
单链表
交集
合并
------解决方案--------------------
------解决方案--------------------
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct node
{
int data;
struct node *next;
}LNode,*LinkList;
LinkList create_LinkList()
{
int x;
LinkList L;
LNode *r;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
r=L;
printf("input the value x:\n");
scanf("%d",&x);
while(x)
{
r->next=(LNode *)malloc(sizeof(LNode));
r->next->data=x;
r=r->next;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
LinkList In_LinkList(LinkList A,LinkList B)
{
LinkList C;
LNode *p,*q,*m;
C=(LinkList)malloc(sizeof(LNode));
C->next=NULL;
m=C;
p=A->next;
q=B->next;
while(p&&q)
{
if(p->data==q->data)
{
m->next=p;
m=p;
m->next=NULL;
p=p->next;
q=q->next;
}
else
q=q->next;
}
return C;
}
void Print_LinkList(LinkList p)
{
LNode *q=p->next;
while(q)
{
printf("%3d",q->data);
q=q->next;
}
printf("\n");
}
void main()
{
LinkList A,B,C; //这里你定义了C C的类型是指针 但是你都没有给他申请空间
C->next=NULL; ///而这里你就直接开始用next了 C都没有空间你怎么给next赋值 显然不行的