假设有两个按元素值递增有序排列的线性表 A 和 B，均以单链表作存储结构，请编写算 法将 A 表和 B 表归并成一个按元素值递减有序（即非递增有序，允许表中含有值相同的元素） 排列的线性表 C，并要求

1.先定义一个结构体。

typedef struct Node
{
int data;
struct Node*next;
}Node,*List;

2.给这个结构体初始化，即得到一个头结点

void InitList(List plist)
{
assert(plist != NULL);
plist->next = NULL;
}

3.用尾插法给两个单链表插入数据

bool Insert_tail(List plist,int val)
{
//第一步，创建新节点
Node *p = (Node *)malloc(sizeof(Node));
assert(p != NULL);
if(p == NULL)
return false;
p->data = val;
//第二步，找尾节点
Node *q;
for(q=plist;q->next!=NULL;q=q->next)
;
//将p插入在q的后面
p->next = q->next;//p->next = NULL;
q->next = p;

return true;
}

4.实现两个递增有序单链表合并成另外一个有序递减的单链表。

void Merge(List plistC,List plistA,List plistB)
{
Node *s = plistA->next;
Node *q = plistB->next;
while(plistB->next != NULL&&plistA->next != NULL)
{
s = plistA->next;
q = plistB->next;
if(s->data <= q->data)
{
plistA->next = s->next;
s->next = NULL;
s->next = plistC->next;
plistC->next = s;
}
else
{
plistB->next = q->next;
q->next = NULL;
q->next = plistC->next;
plistC->next = q;
}
}
if(plistA->next == NULL)
{

while(plistB->next != NULL)
{
q = plistB->next;
plistB->next = q->next;
q->next = plistC->next;
plistC->next = q;
}
}
else
{

while(plistA->next != NULL)
{
s = plistA->next;
plistA->next = s->next;
s->next = plistC->next;
plistC->next = s;
}
}

}

5.打印函数，把所有节点输出。

void Show(List plist)
{
for(Node *p=plist->next;p!= NULL;p=p->next)
{
printf("%d ",p->data);
}
printf("\n");
}

6.摧毁这个链表的节点，防止内存泄漏。

void Destroy(List plist)
{
assert(plist != NULL);
Node *p;
while(plist->next != NULL)
{
p = plist->next;
plist->next = p->next;
}
free(p);
}

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

typedef struct Node
{
int data;
struct Node*next;
}Node,*List;

void InitList(List plist)
{
assert(plist != NULL);
plist->next = NULL;
}

bool Insert_tail(List plist,int val)
{
//第一步，创建新节点
Node *p = (Node *)malloc(sizeof(Node));
assert(p != NULL);
if(p == NULL)
return false;
p->data = val;
//第二步，找尾节点
Node *q;
for(q=plist;q->next!=NULL;q=q->next)
;
//将p插入在q的后面
p->next = q->next;//p->next = NULL;
q->next = p;

return true;
}

void Merge(List plistC,List plistA,List plistB)
{
Node *s = plistA->next;
Node *q = plistB->next;
while(plistB->next != NULL&&plistA->next != NULL)
{
s = plistA->next;
q = plistB->next;
if(s->data <= q->data)
{
plistA->next = s->next;
s->next = NULL;
s->next = plistC->next;
plistC->next = s;
}
else
{
plistB->next = q->next;
q->next = NULL;
q->next = plistC->next;
plistC->next = q;
}
}
if(plistA->next == NULL)
{

while(plistB->next != NULL)
{
q = plistB->next;
plistB->next = q->next;
q->next = plistC->next;
plistC->next = q;
}
}
else
{

while(plistA->next != NULL)
{
s = plistA->next;
plistA->next = s->next;
s->next = plistC->next;
plistC->next = s;
}
}

}

void Show(List plist)
{
for(Node *p=plist->next;p!= NULL;p=p->next)
{
printf("%d ",p->data);
}
printf("\n");
}

void Destroy(List plist)
{
assert(plist != NULL);
Node *p;
while(plist->next != NULL)
{
p = plist->next;
plist->next = p->next;
}
free(p);
}

int main()
{
int i;
for(i=0;i<5;i++)
{
}
for(i=3;i<8;i++)
{
}
return 0;
}

• 22
点赞
• 130
收藏
觉得还不错? 一键收藏
• 7
评论
04-06 550
11-29
07-23 1687
08-21 2822

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。