数据结构之顺序存储结构1

//----------------------------------------------
//定义头文件C.h
#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;

//--------------------------------------------
//主函数main1-1;
#include "C.h"
#include "Sqlist.h"

typedef int T;

bool equal(int c1, int c2)
{
	return c1 == c2;
}

bool equal(double c1, double c2)
{
	return abs(c1 - c2) < 1.0e-6;
}

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

void main()
{
	bool i;
	int j, k;
	T e, e0;
	cout << "---------------------------------------------------------------" << endl;
	SqList<T> L;//顺序表类的对象;
	for (j = 1; j <= 5; j++)
		L.ListInsert(1,j);
	cout << "在L的表头依次插入1-5后,L=";
	L.ListTraverse(print);
	cout << "---------------------------------------------------------------" << endl;
	cout << "L是否为空?" << boolalpha << L.ListEmpty() << ",表长 = " << L.ListLength() << endl;
	L.GetElem(4,e);
	cout << "第4个元素的值为" << e << endl;
	for (j = L.ListLength(); j <= L.ListLength() + 1; j++)
	{
		k = L.LocateElem(j,equal);//查找表L中与j相等的元素,并将其位序赋给k
		if (k)
			cout << "值为" << j << "的元素是表L的第" << k << "个元素,";
		else
			cout << "没有值为" << j << "的元素\n";
	}
	cout << "---------------------------------------------------------------" << endl;
	for (j = 1; j <= 2; j++)
	{
		L.GetElem(j, e);
		i = L.PriorElem(e,equal,e0);
		if (i)
			cout << "元素" << e << "的前驱为" << e0 << endl;
		else
			cout << "元素" << e << "无前驱,";
	}
	cout << "---------------------------------------------------------------" << endl;
	for (j = L.ListLength() - 1; j <= L.ListLength(); j++)
	{
		L.GetElem(j, e);
		i = L.NextElem(e,equal,e0);
		if (i)
			cout << "元素" << e << "的后继为" << e0;
		else
			cout << ", 元素" << e << "无后继" << endl;
	}
	cout << "---------------------------------------------------------------" << endl;
	k = L.ListLength();
	for (j = k + 1; j >= k; j--)
	{
		i = L.ListDelete(j, e);
		if (i)
			cout << "删除第" << j << "个元素成功,其值为" << e;
		else
			cout << "删除第" << j << "个元素失败。";
	}
	L.ClearList();
	cout << endl << "清空L后, L是否空?" << boolalpha << L.ListEmpty();
	cout << ", 表长 = " << L.ListLength() << endl;
	cout << "---------------------------------------------------------------" << endl;



	system("pause");
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值