顺序表C++版本

模板类头文件.h
#pragma once
#include"iostream"

using namespace std;

template<typename T>
class SeqList
{
public:
	SeqList();
	SeqList(int capacity);
	~SeqList();
	void SeqlistClear();
	int getlen();//返回长度
	int getCapacity();//返回容量
	int insert(int pos,T&node);//指定位置插入节点
    int getNode(int pos,T&node);//获取指定位置节点
	int SeqlistDelete(int pos, T&node);//删除指定位置节点

private:
	int capacity;
	int len;
	T *pArrey;

};

//hpp文件,实现类模板的具体成员函数

#include"dm_10_seqlistTemplate.h"

using namespace std;

template<typename T>
SeqList<T>::SeqList(int capacity)//有参构造函数 创建顺序表
{
	this->pArrey = new T[capacity];
	this->capacity = capacity;
	this->len = 0;
}
template<typename T>
SeqList<T>::~SeqList()//析构类 销毁顺序表
{
	if (this->pArrey != NULL)
	{
		delete []pArrey;//析构函数自动销毁链表
	}
}

template<typename T>//清空顺序表
void SeqList<T>::SeqlistClear() {
	if (pArrey == NULL)
	{
		cout << "传入顺序表为空" << endl;
		return;
	}
	this->len = 0;//清空链表即回到初始化状态,len=0
}

template<typename T>
int SeqList<T>:: getlen()//返回长度
{
	return this->len ;
}
template<typename T>
int  SeqList<T>::getCapacity()//返回容量
{
	return this->capacity;
}

template<typename T>
int  SeqList<T>::insert(int pos, T &node)//指定位置插入节点
{
	int ret = 0;
	//输入不合法
	if (pos < 0)
	{
		ret = -1;
		cout << "输入不合法 pos < 0 " << ret << endl;
		return ret;
	}
	//顺序表已经满了
	if (this->len == this->capacity)
	{
		ret = -2;
		cout << "顺序表已经满了len == capacity " << ret << endl;
		return ret;
	}
	//顺序表没有满 插入位置大于len 即1到4 插到6、位置5空了
	if (pos >= this->len)
	{
		pos = this->len;
	}
	//移动后面元素
	for(int i = this->len; i > pos; i--)
	{
		this->pArrey[i] = this->pArrey[i - 1];
	}
	//插入元素
	pArrey[pos] = node;//c++直接赋值 必须支持赋值 若有指针则必须重载=避免深拷贝浅拷贝						
	this->len++;
	return ret;
}

template<typename T>
int SeqList<T>::getNode(int pos, T&node)//获取指定位置节点
{

	//输入不合法
	int ret = 0;
	if ( pos < 0)
	{
		ret = -1;
		cout << "输入不合法pos < 0" << ret << endl;
		return ret;
	}
	node = this->pArrey[pos];
	return ret;
}
template<typename T>
int SeqList<T>::SeqlistDelete(int pos, T&node)//删除指定位置节点
{
	int ret = 0;
	//输入不合法
	if (pos < 0)
	{
		ret = -1;
		cout << "输入不合法  pos < 0 " << ret << endl;
		return ret;
	}
	//移动后面元素
	node = this->pArrey[pos];
	for (int i = pos + 1; i <this->len; i++)
	{
		this->pArrey[i - 1] = this->pArrey[i];
	}
	this->len--;
	return ret;
}

//main程序测试程序
#define  _CRT_SECURE_NO_WARNINGS
#include"dm_10_seqlistTemplate.hpp"
#include"iostream"

using namespace std;


struct Teacher10 {
	int age;
	char name[32];
};
void main()
{
	SeqList<Teacher10>seqTlist(10);//构造函数直接建立链表无需专门建立链表
	int ret = 0;
	Teacher10 t1, t2, t3, t4;
	strcpy(t1.name, "zhangsan10");
	strcpy(t2.name, "lisi10");
	strcpy(t3.name, "wangwu10");
	strcpy(t4.name, "maliu10");
	t1.age = 20;
	t2.age = 22;
	t3.age = 24;
	t4.age = 26;
	
	//头插

	ret = seqTlist.insert(0,t1);//链表不关心具体的节点是啥(void),用到具体类型进行强制类型转换
	ret = seqTlist.insert(0, t2);
	ret = seqTlist.insert(0, t3);
	ret = seqTlist.insert(0, t4);

	//遍历
	for (int i = 0; i < seqTlist.getlen(); i++)
	{
		Teacher10 tmp;//上层应用知道自己传的是Teacher,别人不知道什么类型,
		ret = seqTlist.getNode(i,tmp);
		if (ret!=0)
		{
			ret = -2;
			cout << "func getNode err" << ret << endl;
			return;
		}
		cout << tmp.name << tmp.age << endl;
	}
	//删除
	while (seqTlist.getlen()>0)
	{
		Teacher10 tmp ;
		ret = seqTlist.SeqlistDelete(0, tmp);

		if (ret != 0)
		{
			ret = -3;
			cout << "func seqTlist.SeqlistDelete( err" << ret << endl;
			return;
		}
		cout << tmp.name << '\t' << tmp.age << endl;
	}


	for (int i = 0; i < seqTlist.getlen(); i++)
	{
		Teacher10 tmp;//上层应用知道自己传的是Teacher,别人不知道什么类型,
		ret = seqTlist.getNode(i, tmp);
		if (ret != 0)
		{
			ret = -3;
			cout << "func seqTlist.getNode( err" << ret << endl;
			return;
		}
		cout << tmp.name << tmp.age << endl;
	}

}
c++思想写的,mark一下,泛型编程,模板类,可以处理任何数据类型

猜你喜欢

转载自blog.csdn.net/WSTONECH/article/details/81045574