问题遇到的现象和发生背景
初学链表,想用两个顺序链表line与line2合成一个lin,使其合成为一个顺序表,假设line与line2无重复元素,为啥lin输出不了。
用代码块功能插入代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
struct node
{
int a;
struct node* llink;
struct node* rlink;
};
struct q
{
int max;
node* head;
};
q line,line2;
int init(q* h)
{
h->head = (node*)malloc(sizeof(node));
if (!h->head)
return 0;
h->head->rlink = NULL;
h->head->llink = NULL;
return 1;
}
void newnode(int key, q* h)
{
node* m,*n;
m = h->head;
if (m == NULL)
{
m->a = key;
m->llink = NULL;
}
else
{
while (m->llink != NULL)
{
m = m->llink;
}
n = (node*)malloc(sizeof(node));
n->a = key;
m->llink = n;
n->rlink = m;
n->llink = NULL;
}
}
void out(q h)
{
node* m;
m = h.head;
while(m->llink != NULL)
{
m = m->llink;
printf("%d ", m->a);
}
}
q lin;
void donode(q* h,q*lin)
{
node* m, * n,*l;
m = h->head->llink;
n = line2.head->llink;
l = lin->head;
while (1 )
{
if (m->a < n->a)
{
l = m;
m = m->llink;
}else
{
l = n;
n = n->llink;
}
if (n == NULL)
{
l->llink = m;
break;
}
if (m == NULL)
{
l->llink = n;
break;
}
l = l->llink;
l = (node*)malloc(sizeof(node));
}
}
int main()
{
int n;
scanf("%d", &n);
int t=init(&line);
if (t == 1) printf("success!\n");
else printf("Fail!\n");
for (int i = 1; i <= n; i++)
{
int key;
scanf("%d", &key);
newnode(key, &line);
}
scanf("%d", &n);
t = init(&line2);
if (t == 1) printf("success!\n");
else printf("Fail!\n");
for (int i = 1; i <= n; i++)
{
int key;
scanf("%d", &key);
newnode(key, &line2);
}
t = init(&lin);
if (t == 1) printf("success!\n");
donode(&line,&lin);
out(lin);
return 0;
}