为什么运行结果莫名其妙多了一个0,用递归做的
typedef struct node{
int data;
struct node* next;
}Node,*Linklist;
Linklist Create(Linklist L,int n)
{
int a[n];
Node *r,*node;
L=(Node*)malloc(sizeof(Node));
L->next=NULL;
r=L;
for(int i=0;idata=a[i];
r->next=node;
node=r;
}
r->next=NULL;
}
void Init(Linklist &L)
{
L=(Linklist)malloc(sizeof(Linklist));
L->next=NULL;
}
bool Insert(Linklist L,int i,int x)
{
Node *p=L,*node;
int count=0;
while(p!=NULL&&countp=p->next;
count++;
}
if(p==NULL)
return false;
else
{
node=(Node*)malloc(sizeof(Node));
node->data=x;
node->next=p->next;
p->next=node;
return true;
}
}
Linklist merge(Linklist &L1,Linklist &L2)
{
Node* head;
if(L1==NULL)
{
return L2;
}
else if(L2==NULL)
{
return L1;
}
else
{
if(L1->datadata)
{
head=L1;
head->next=merge(L1->next,L2);
}
else {
head=L2;
head->next=merge(L1,L2->next);
}
return head;
}
}
void Display(Linklist L)
{
Node *p;
p=(Node*)malloc(sizeof(Node));
p=L->next;
while(p!=NULL)
{
printf(" %d ",p->data);
p=p->next;
}
printf("\n");
}
void Destory(Linklist &L)
{
Node *p=L,*q=p->next;
while(q!=NULL)
{
free(q);
p=q;
q=p->next;
}
}
你写的这个Create就很迷,数组a都没有赋值,你写node->data=a[i];有什么意义吗,为什么要先创建一个长度是n的随机数链表,怎么不一边合并一边往下创建Node呢
merge函数也很迷,head一开始不是L1就是L2,然后一直往下链,这不是直接把L1挂到L2后面或者把L2挂到L1后面了吗,跟你create出来的链表有什么关系吗
L1和L2本身不要修改,否则后续的元素你访问不到了