假设有两个按元素值递增有序排列的线性表 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()
{
    Node head1;
    Node head2;
	Node head3;
    InitList(&head1);
    InitList(&head2);
	InitList(&head3);
    int i;
    for(i=0;i<5;i++)
    {
	Insert_tail(&head1,i);
    }
    for(i=3;i<8;i++)
    {
	Insert_tail(&head2,i);
    }
    Show(&head1);
	Show(&head2);
	Merge(&head3,&head1,&head2);
	Show(&head3);
	Destroy(&head3);
    return 0;
}

 

  • 22
    点赞
  • 130
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值