数据结构之有序链表归并算法2

本文介绍了一种使用C++模板实现线性表的方法,包括抽象基类AList和具体实现类SqList。通过模板机制实现了对不同类型数据的支持,并提供了基本操作如插入、删除等。此外,还展示了如何通过继承来扩展功能,例如两个线性表的合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//头文件定义;
#ifndef _C_H_
#define _C_H_

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <ctime>
#include <cstdarg>
#include <assert.h>
using namespace std;

#endif;

 
//线性抽象类定义;
#ifndef _ALIST_H_
#define _ALIST_H_


template<typename T> class AList
{ 
public:
	void ClearList();
	bool ListEmpty() const;
	int LocateElem(T e, bool(*eq)(T, T)) const;
	bool PriorElem(T e, bool(*eq)(T, T), T &pre_e) const;
	bool NextElem(T e, bool(*eq)(T, T), T &next_e) const;
	bool ListDelete(int i, T &e);
	void ListTraverse(void(*visit)(T*)) const;
	virtual bool GetElem(int i, T &e) const = 0;//纯虚函数;
	virtual bool ListInsert(int i, T e) = 0;
	virtual int ListLength() const = 0;
};

#endif;

//顺序链表类定义;
#ifndef _SQLIST_H_
#define _SQLIST_H_


#include "AList.h"
//模板类继承;
template<typename T>class SqList :public AList < T >
{
	friend void MergeList(const SqList<T>&, const SqList<T>&, SqList<T>&);
private:
	T *elem;
	int length;//线性表的当前长度;
	int listsize;//线性表的当前存储容量;

public:
	SqList(int i = 1)
	{
		elem = new T[i];
		assert(elem != NULL);
		length = 0;
		listsize = i;
	}

	~SqList()
	{
		delete[]elem;
	}

	void ClearList()
	{
		length = 0;//重置线性表为空;
	}

	bool ListEmpty() const//函数名后边加上const表示函数内部不能改变类中成员变量的值;
	{
		return length == 0;
	}

	int ListLength() const
	{
		return length;
	}

	bool GetElem(int i, T &e) const
	{
		if (i < 1 || i > length)
			return false;
		e = *(elem + i - 1);
		return true;
	}

	int LocateElem(T e, bool(*eq)(T, T)) const
	{
		int i = 1;
		while (i <= length && !eq(*(elem + i - 1), e))
			i++;
		if (i <= length)
			return i;
		else
			return 0;
	}

	bool PriorElem(T e, bool(*eq)(T, T), T &pre_e) const
	{
		int i = LocateElem(e,eq);
		if (i <= 1)
			return false;//寻找失败,元素不存在或者为第一个元素;
		else
		{
			pre_e = *(elem + i - 2);
			return true;
		}
	}

	bool NextElem(T e, bool(*eq)(T, T), T &next_e) const
	{
		int i = LocateElem(e,eq);
		if (i == 0 || i == length)
			return false;
		else
		{
			next_e = *(elem + i);
			return true;
		}
	}

	bool ListInsert(int i, T e)
	{
		T *newbase, *q, *p;
		if (i < 1 || i > length + 1)
			return false;
		if (length == listsize)
		{
			newbase = new T[listsize * 2];
			for (int j = 0; j < length; j++)
				*(newbase + j) = *(elem + j);
			delete[]elem;//释放原表空间;
			elem = newbase;
			listsize *= 2;
		}
		q = elem + i - 1;
		for (p = elem + length - 1; p >= q; p--)
			*(p + 1) = *p;
		*q = e;
		++length;
		return true;
	}

	bool ListDelete(int i, T &e)
	{
		T *p, *q;
		if (i < 1 || i > length)
			return false;
		p = elem + i - 1;
		e = *p;
		q = elem + length - 1;
		for (++p; p <= q; p++)
			*(p - 1) = *p;
		length--;
		return true;
	}

	void ListTraverse(void(*visit)(T*)) const;
};


template<typename T>
void SqList<T>::ListTraverse(void(*visit)(T*)) const
{
	for (int i = 0; i < length; i++)
	{
		visit(elem + i);
	}
	cout << endl;
}

#endif;

//主函数;
#include "C.h"
#include "SqList.h"
#include "LinkList.h"
typedef int T;

void print(T* c)
{
	cout << *c << " ";
}

void AMergeList(const AList<T>& La, const AList<T>& Lb, AList<T>& Lc)
{
	int len_a = La.ListLength();
	int len_b = Lb.ListLength();
	int i = 1;
	int j = 1;
	int k = 1;

	T ea, eb;

	while (i <= len_a && j <= len_b)
	{
		La.GetElem(i,ea);
		Lb.GetElem(j,eb);

		if (ea <= eb)
		{
			Lc.ListInsert(k,ea);
			i++;
			k++;
		}
		else
		{
			Lc.ListInsert(k,eb);
			j++;
			k++;
		}
	}

	while (i <= len_a)
	{
		La.GetElem(i,ea);
		Lc.ListInsert(k,ea);
		i++;
		k++;
	}

	while (j <= len_b)
	{
		Lb.GetElem(j,eb);
		Lc.ListInsert(k,eb);
		j++;
		k++;
	}
}

void main()
{
	SqList<T> La(5), Lb(5), Lc(10);

	for (int i = 1; i <= 5; i++)
	{
		La.ListInsert(i,i);
		Lb.ListInsert(i,2 * i);
	}

	cout << "La = ";
	La.ListTraverse(print);
	cout << "Lb = ";
	Lb.ListTraverse(print);

	AMergeList(La, Lb, Lc);
	cout << "Lc = ";
	Lc.ListTraverse(print);

	system("pause");
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值