不知道为什么Lc无法打印
#include
#include
#include
typedef struct node {//定义结点
int data;//结点数据域
struct node* next;//结点指针域
}node, * Linklist;
void creatList_1(node* l,int n) {//头插法
l->next = NULL;
node *p;//设置一个结点指针
int i = 1;
int j;
for (i = n;i > 0;--i) {
p = (Linklist)malloc(sizeof(node));//动态分配内存
scanf_s("%d", &j);
p->data = j;
p->next = l->next;
l->next = p;
}
}
void creatList_2(node* l,int n) {
l->next = NULL;
node* p;
node* r;
int i = 1;
int j;
r = l;
for(i=1;i<=n;++i){
p = (Linklist)malloc(sizeof(node));
scanf_s("%d", &j);
p->data = j;
p->next = r->next;
r->next = p;//尾指针的指针域指向新结点p
r = p;//尾指针永远在最后
}
}
void MergeList_L(node* l1, node* l2, node* l3) {
node* p1, * p2, * p3;
p1 = l1->next;
p2 = l2->next;
p3 = l1;
l3 = p3;
while (p1 && p2) {
if (p1->data<= p2->data)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p1 ? p1 : p2;
free(l2);
}
void PrintList(node* l) {
printf("内容为");
while (l->next != NULL)
{
printf("%d ", l->next->data);
l = l->next;
}
printf("\n");
}
void main() {
Linklist La, Lb, Lc;
La = (node*)malloc(sizeof(node));//动态分配内存
Lb = (node*)malloc(sizeof(node));
Lc = (node*)malloc(sizeof(node));
Lc->next = NULL;
creatList_1(La,7);//头插法,倒序
creatList_2(Lb,5);//尾插法,正序
MergeList_L(La, Lb, Lc);
PrintList(Lc);
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node {//定义结点
int data;//结点数据域
struct node* next;//结点指针域
}node, * Linklist;
void creatList_1(node* l,int n) {//头插法
l->next = NULL;
node *p;//设置一个结点指针
int i = 1;
int j;
for (i = n;i > 0;--i) {
p = (Linklist)malloc(sizeof(node));//动态分配内存
scanf_s("%d", &j);
p->data = j;
p->next = l->next;
l->next = p;
}
}
void creatList_2(node* l,int n) {
l->next = NULL;
node* p;
node* r;
int i = 1;
int j;
r = l;
for(i=1;i<=n;++i){
p = (Linklist)malloc(sizeof(node));
scanf_s("%d", &j);
p->data = j;
p->next = r->next;
r->next = p;//尾指针的指针域指向新结点p
r = p;//尾指针永远在最后
}
}
void MergeList_L(node* l1, node* l2, node** l3) {
node* p1, * p2, * p3;
p1 = l1->next;
p2 = l2->next;
p3 = l1;
while (p1 && p2) {
if (p1->data<= p2->data)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p1 ? p1 : p2;
free(l2);
*l3 = l1;
}
void PrintList(node* l) {
printf("内容为");
while (l->next != NULL)
{
printf("%d ", l->next->data);
l = l->next;
}
printf("\n");
}
void main() {
Linklist La, Lb, Lc;
La = (node*)malloc(sizeof(node));//动态分配内存
Lb = (node*)malloc(sizeof(node));
creatList_1(La,3);//头插法,倒序
creatList_2(Lb,3);//尾插法,正序
MergeList_L(La, Lb, &Lc);
PrintList(Lc);
}
改动处见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node {//定义结点
int data;//结点数据域
struct node* next;//结点指针域
}node, * Linklist;
void creatList_1(node* l,int n) {//头插法
l->next = NULL;
node *p;//设置一个结点指针
int i = 1;
int j;
for (i = n;i > 0;--i) {
p = (Linklist)malloc(sizeof(node));//动态分配内存
scanf_s("%d", &j);
p->data = j;
p->next = l->next;
l->next = p;
}
}
void creatList_2(node* l,int n) {
l->next = NULL;
node* p;
node* r;
int i = 1;
int j;
r = l;
for(i=1;i<=n;++i){
p = (Linklist)malloc(sizeof(node));
p->next = NULL; //修改
scanf_s("%d", &j);
p->data = j;
//p->next = r->next; 修改
r->next = p;//尾指针的指针域指向新结点p
r = p;//尾指针永远在最后
}
}
void MergeList_L(node* l1, node* l2, node* l3) {
node* p1, * p2, * p3;
p1 = l1->next;
p2 = l2->next;
p3 = l3; //p3 = l1;修改
//l3 = p3;修改
while (p1 && p2) {
if (p1->data<= p2->data)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p1 ? p1 : p2;
free(l2);
free(l1); //修改
}
void PrintList(node* l) {
printf("内容为");
while (l->next != NULL)
{
printf("%d ", l->next->data);
l = l->next;
}
printf("\n");
}
void main() {
Linklist La, Lb, Lc;
La = (node*)malloc(sizeof(node));//动态分配内存
Lb = (node*)malloc(sizeof(node));
Lc = (node*)malloc(sizeof(node));
Lc->next = NULL;
creatList_1(La,7);//头插法,倒序
creatList_2(Lb,5);//尾插法,正序
MergeList_L(La, Lb, Lc);
PrintList(Lc);
}