关于线性表(顺序表&链表)的合并问题


一、在顺序表中实现两个有序的稀疏多项式相加

代码如下:


#include<iostream>
#include<math.h>
using namespace std;
#define NUM 10
#pragma warning(disable:4996)
typedef struct student
{
	int name;
	int num;
}stu;
typedef struct Student
{
	char name[20];
	int num;
}Stu;
typedef struct liist//定义一个这样的访问线性表的结构体指针和测量长度的变量
{
	stu *p;
	int length;
}List;
typedef struct llist//定义一个这样的访问线性表的结构体指针和测量长度的变量
{
	Stu *p;
	int length;
}list;
void connectlist(list &La, list &Lb);//顺序表合并
void printflist(List &L);//数学公式打印
List addlist(List &La, List &Lb);//顺序表有序合并,借助一个空表Lc
int main()
{
	//顺序表的有序合并
	stu math1[5];
	stu math2[5];
	List L1, L2;
	open(L1); open(L2);
	math1[0].name = 2; math1[0].num = 1;
	math1[1].name = 3; math1[1].num = 5;
	math1[2].name = 5; math1[2].num = 8;
	for (int i = 0; i < 3; i++)
	{
		insertlist(L1, math1[i]);
	}
	math2[0].name = 1; math2[0].num = -5;
	math2[1].name = 3; math2[1].num = 7;
	math2[2].name = 5; math2[2].num = -8;
	for (int i = 0; i < 3; i++)
	{
		insertlist(L2, math2[i]);
	
	}
	printflist(L1); cout << endl<<'+'<<endl; printflist(L2);
	cout << endl<<'=';
	List L3=addlist(L1, L2);
	printflist(L3);
	cout << endl;
	
	/*顺序表合并
	Stu student1[5];
	Stu student2[5];
	list L4, L5;
	Open(L4); Open(L5);
	strcpy(student1[0].name, "张三"); student1[0].num = 1;
	strcpy(student1[1].name, "张四"); student1[1].num = 8;
	strcpy(student1[2].name, "张五"); student1[2].num = 7;
	for (int i = 0; i < 3; i++)
	{
		Insertlist(L4, student1[i]);
	}
	strcpy(student2[0].name, "张六"); student2[0].num = 2;
	strcpy(student2[1].name, "张七"); student2[1].num = 5;
	for (int i = 0; i < 2; i++)
	{
		Insertlist(L5, student2[i]);
	}
	connectlist(L4, L5);
	showlist(L4);*/
	if (!L1.p) {
		delete[] L1.p; L1.p = NULL;

	}
	if (!L2.p) {
		delete[] L2.p; L2.p = NULL;

	}
	if (!L3.p) {
		delete[] L3.p; L3.p = NULL;

	}
	return 0;
}
void connectlist(list &La, list &Lb)//顺序表合并
{
	int j = 0;
	int a = La.length + Lb.length;
	for (int i = La.length; i < a; i++)
	{

		La.p[i] = Lb.p[j];
		++La.length;
		j++;
	}

}
List addlist(List &La, List &Lb)//顺序表有序合并,借助一个空表Lc
{
	List Lc; open(Lc);
	int count = 0;
	int matha = 0, mathb = 0;
	int i = 0, j = 0;
	while (1)
	{

		if (i == La.length) { matha = 0; break; }
		else if (j == Lb.length) { matha = 1; break; }
		if (La.p[i].name > Lb.p[j].name) Lc.p[count++] = Lb.p[j++];
		else if (La.p[i].name < Lb.p[j].name) Lc.p[count++] = La.p[i++];	
		else
		{
			if (La.p[i].num + Lb.p[j].num == 0)
			{
				i++; j++;
			}
			else 
			{
				Lc.p[count].name = Lb.p[j].name;
				Lc.p[count].num = La.p[i].num + Lb.p[j].num;
				count++;
				i++; j++;
			}

		}

	}

	if (matha == 0)
	{
		for (; j < Lb.length; j++) Lc.p[count++] = Lb.p[j];
	}
			
	else if (matha == 1)
	{ 
		for (; i < La.length; i++)
		
			Lc.p[count++] = La.p[i];	
	}
	Lc.length = count;
	return Lc;
}
void printflist(List &L)//数学公式打印
{
	for (int i = 0; i < L.length; i++)
	{
		if (i == L.length - 1) cout << L.p[i].num << "*x^" << L.p[i].name << endl;
		if(L.p[i+1].num>0&&i!=L.length-1) cout << L.p[i].num << "*x^" << L.p[i].name << '+';
		if (L.p[i + 1].num < 0 && i != L.length-1) cout << L.p[i].num << "*x^" << L.p[i].name;
	}
}

在这里插入图片描述
这是上述代码对应的运行结果


二、在链表中将两个有序链表合并成一个有序链表

代码如下

#include<iostream>
#include<time.h>
#include<Windows.h>
using namespace std;
#define NUM 100
#pragma warning(disable:4996)
typedef struct student
{
	int num;
	char name[10];
	float score;
}stu;
typedef struct list
{
	stu data;
	struct list *next;
}List;
void Connectlist(List *La, List *Lb, List *&Lc);//有序链表的合并
int main()
{
	List *L1 = opennode();
	List *L2 = opennode();
	List *L3 = opennode();
	stu student1[5];
	stu student2[5];
	student1[0].num = 2020120606; student1[0].score = 134; strcpy(student1[0].name, "小明");
	student1[1].num = 2020120604; student1[1].score = 140; strcpy(student1[1].name, "小刚");
	student1[2].num = 2020120601; student1[2].score = 134; strcpy(student1[2].name, "小红");
	student2[0].num = 2020120603; student2[0].score = 124.5; strcpy(student2[0].name, "小胡");
	student2[1].num = 2020120602; student2[1].score = 141; strcpy(student2[1].name, "小夏");
	for (int i = 0; i < 3; i++)
	{
		insertlist(L1, student1[i]);
	}
	for (int i = 0; i < 2; i++)
	{
		insertlist(L2, student2[i]);
	}
	printflist(L1); printflist(L2);
	Connectlist(L1, L2, L3); printflist(L3);//对两个有序的链表进行合并后使其依然有序,并将其输出
	destroylist(L3);
	return 0;
}
void Connectlist(List *La, List *Lb, List *&Lc)//有序链表的合并
{
	Lc = La;
	List *pc = Lc;
	List *pa = La->next;
	List *pb = Lb->next;
	while (pa&&pb)
	{
		if (pa->data.num <= pb->data.num)
		{
			pc->next = pa;
			pc = pa;
			pa = pa->next;
		}
		else
		{
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
	}
	pc->next = pa ? pa : pb;
	delete Lb;

}

在这里插入图片描述
这是上述代码对应的运行结果

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值