![]() |
#2
fulltimelink2020-05-19 08:38
先让pb1指向b链表的下一个,再改动pb2指向a链表
如果先改pb2, pb1->next就移动到a链表去了 pb1 = pb1->next; pb2->next = pa1; ![]() #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct student) typedef struct student { int num; int score; struct student *next; }stu; void print(stu *head); void insert(stu *head, stu *p); stu *creat(); void clear(stu *head); stu *combine(stu *ahead, stu *bhead); void main() { stu *ahead, *bhead; printf("input list a:\n"); ahead = creat(); //建立链表a printf("List a:\n"); print(ahead); printf("\n\ninput list b:\n"); bhead = creat(); printf("List b:\n"); //建立链表b print(bhead); ahead = combine(ahead, bhead); //合并链表 printf("\n\nList:\n"); print(ahead); clear(ahead); //清除 } stu *creat() { stu *p, *head; head = (stu *)malloc(LEN); //建立头结点 head->next = NULL; printf("input number,score:\n"); p = (stu *)malloc(LEN); //建立数据节点 scanf("%d %d", &p->num, &p->score); //输入数据 p->next = NULL; while (p->num != 0) { insert(head, p); //按顺序输入 p = (stu *)malloc(LEN); scanf("%d %d", &p->num, &p->score); } free(p); return(head); } void insert(stu *head, stu *p) { stu *p0, *p1, *p2; p2 = head; p1 = head->next; p0 = p; while (p1 != NULL) { if (p0->num > p1->num) //让p0在p1和p2之间 { p2 = p1; p1 = p1->next; //p1后移 } else break; } p0->next = p1; //p0,p1链接 p2->next = p0; //p2,p0链接 } stu *combine(stu *ahead, stu *bhead) { stu *pa1, *pa2, *pb1, *pb2; pa2 = ahead; pa1 = ahead->next; pb2 = pb1 = bhead->next; //头结点用不到 while (pb1&&pa1->next != NULL) { while ((pb1->num > pa1->num) && (pa1->next != NULL)) { pa2 = pa1; //往后移 pa1 = pa1->next; //后移 } if (pb1->num <= pa1->num) { pa2->next = pb1; pb1 = pb1->next; pb2->next = pa1; //节点连接 //第二个链表后移 pa2 = pb2; //pa1移动 pb2 = pb1; //pb2后移 } } if (pb1) //非空已指向链表b尾 pa1->next = pb1; //b链表剩下的接到a链表后边 free(bhead); return(ahead); } void print(stu *head) { stu *p; p = head->next; while (p != NULL) { printf("%d %d\n", p->num, p->score); p = p->next; } } void clear(stu *head) { stu *p; p = head->next; while (p != NULL) { head->next = p->next; free(p); p = head->next; } free(head); } |

//已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排序。
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
typedef struct student
{
int num;
int score;
struct student *next;
}stu;
void print(stu *head);
void insert(stu *head,stu *p);
stu *creat();
void clear(stu *head);
stu *combine(stu *ahead,stu *bhead);
void main()
{
stu *ahead,*bhead;
printf("input list a:\n");
ahead=creat(); //建立链表a
printf("List a:\n");
print(ahead);
printf("\n\ninput list b:\n");
bhead=creat();
printf("List b:\n"); //建立链表b
print(bhead);
ahead=combine(ahead,bhead); //合并链表
printf("\n\nList:\n");
print(ahead);
clear(ahead); //清除
}
stu *creat()
{
stu *p,*head;
head=(stu *)malloc(LEN); //建立头结点
head->next=NULL;
printf("input number,score:\n");
p=(stu *)malloc(LEN); //建立数据节点
scanf("%d %d",&p->num,&p->score); //输入数据
while(p->num!=0)
{
insert(head,p); //按顺序输入
p=(stu *)malloc(LEN);
scanf("%d %d",&p->num,&p->score);
}
free(p);
return(head);
}
void insert(stu *head,stu *p)
{
stu *p0,*p1,*p2;
p2=head;
p1=head->next;
p0=p;
while(p1!=NULL)
{
if(p0->num>p1->num) //让p0在p1和p2之间
{
p2=p1;
p1=p1->next; //p1后移
}
else break;
}
p0->next=p1; //p0,p1链接
p2->next=p0; //p2,p0链接
}
stu *combine(stu *ahead,stu *bhead)
{
stu *pa1,*pa2,*pb1,*pb2;
pa2=ahead;
pa1=ahead->next;
pb2=pb1=bhead->next; //头结点用不到
while(pb1&&pa1->next!=NULL)
{
while((pb1->num>pa1->num)&&(pa1->next!=NULL))
{
pa2=pa1; //往后移
pa1=pa1->next; //后移
}
if(pb1->num<=pa1->num)
{
pa2->next=pb1;
pb2->next=pa1; //节点连接
pb1=pb1->next; //第二个链表后移
pa2=pb2; //pa1移动
pb2=pb1; //pb2后移
}
}
if(pb1) //非空已指向链表b尾
pa1->next=pb1; //b链表剩下的接到a链表后边
free(bhead);
return(ahead);
}
void print(stu *head)
{
stu *p;
p=head->next;
while(p!=NULL)
{
printf("%d %d\n",p->num,p->score);
p=p->next;
}
}
void clear(stu *head)
{
stu *p;
p=head->next;
while(p!=NULL)
{
head->next=p->next;
free(p);
p=head->next;
}
free(head);
}
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
typedef struct student
{
int num;
int score;
struct student *next;
}stu;
void print(stu *head);
void insert(stu *head,stu *p);
stu *creat();
void clear(stu *head);
stu *combine(stu *ahead,stu *bhead);
void main()
{
stu *ahead,*bhead;
printf("input list a:\n");
ahead=creat(); //建立链表a
printf("List a:\n");
print(ahead);
printf("\n\ninput list b:\n");
bhead=creat();
printf("List b:\n"); //建立链表b
print(bhead);
ahead=combine(ahead,bhead); //合并链表
printf("\n\nList:\n");
print(ahead);
clear(ahead); //清除
}
stu *creat()
{
stu *p,*head;
head=(stu *)malloc(LEN); //建立头结点
head->next=NULL;
printf("input number,score:\n");
p=(stu *)malloc(LEN); //建立数据节点
scanf("%d %d",&p->num,&p->score); //输入数据
while(p->num!=0)
{
insert(head,p); //按顺序输入
p=(stu *)malloc(LEN);
scanf("%d %d",&p->num,&p->score);
}
free(p);
return(head);
}
void insert(stu *head,stu *p)
{
stu *p0,*p1,*p2;
p2=head;
p1=head->next;
p0=p;
while(p1!=NULL)
{
if(p0->num>p1->num) //让p0在p1和p2之间
{
p2=p1;
p1=p1->next; //p1后移
}
else break;
}
p0->next=p1; //p0,p1链接
p2->next=p0; //p2,p0链接
}
stu *combine(stu *ahead,stu *bhead)
{
stu *pa1,*pa2,*pb1,*pb2;
pa2=ahead;
pa1=ahead->next;
pb2=pb1=bhead->next; //头结点用不到
while(pb1&&pa1->next!=NULL)
{
while((pb1->num>pa1->num)&&(pa1->next!=NULL))
{
pa2=pa1; //往后移
pa1=pa1->next; //后移
}
if(pb1->num<=pa1->num)
{
pa2->next=pb1;
pb2->next=pa1; //节点连接
pb1=pb1->next; //第二个链表后移
pa2=pb2; //pa1移动
pb2=pb1; //pb2后移
}
}
if(pb1) //非空已指向链表b尾
pa1->next=pb1; //b链表剩下的接到a链表后边
free(bhead);
return(ahead);
}
void print(stu *head)
{
stu *p;
p=head->next;
while(p!=NULL)
{
printf("%d %d\n",p->num,p->score);
p=p->next;
}
}
void clear(stu *head)
{
stu *p;
p=head->next;
while(p!=NULL)
{
head->next=p->next;
free(p);
p=head->next;
}
free(head);
}
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
[此贴子已经被作者于2020-5-18 23:46编辑过]