C++学习之用数组实现顺序表

本文详细介绍了使用C++的模板技术实现顺序表类模板SequenceList的过程,包括构造、插入、删除、查找、更新和输出等基本操作。通过实例演示了如何在代码中应用这些功能,旨在帮助初学者理解并掌握顺序表的实现原理。

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

开始学习数据结构,路还长,加油!!

用C++的template实现顺序表SequenceList ,第一次上传完整代码,比较菜。


SequenceList.h:

#ifndef SEQ_LIST_H
#define SEQ_LIST_H
#include <iostream>
#include <exception>
//模板的声明和定义要在同一个头文件中

//创建一个顺序表类模板
template <typename Type> class SequenceList
{
public:
	SequenceList(int length = 10);
	~SequenceList();
	//增 可以在某个位置插入某个元素 index 个元素之后插入数据
	void Insert(const int index ,  const Type * value);
	//删
	void Delete(const int index ,  Type * value);
	//查
	int GetIndex(const Type *value);
	//改
	void Update(const int index , Type * value);
	//清空数组
	void OutPut(std::ostream & out) const;
	//返回长度
	int Length();
	bool IsEmpty();
private:
	Type * SList;
	int MaxSize , Len;
};


template <class Type>
SequenceList<Type>::SequenceList(int length = 10)
{
	//构造函数
	SList = new Type[length];
	MaxSize = length;
	Len = 0;
}

template <class Type>
void SequenceList<Type>::Insert(const int index , const Type * value)
{
	//增
	if (index <0 || index > MaxSize)
	{
		throw std::out_of_range("out of bounds!!");
	}
	if(Len == MaxSize)
	{
		throw std::out_of_range("No Memory !!");
	}

	//向后移一个位置
	for(int i = Len ; i >= index ; i--)
	{
		SList[i+1] = SList[i];
	}
	SList[index] = *value;
	Len++;
}

template <class Type>
void SequenceList<Type>::Delete(const int index , Type * value)
{
	//将顺序表中的第K个元素存入value中, 并将第K个元素删除
	if(index < 0 || index > Len) throw std::out_of_range("out of bounds!!");
	*value = SList[index];
	for(int i = index ; i <= Len ;i++)
	{
		SList[i] = SList[i+1];
	}
	Len--;
}

template <class Type>
int SequenceList<Type>::GetIndex(const Type* value)
{
	//查询value所在的位置 不是索引值 如果没有则返回0
	for(int  i = 0 ;i < Len ;i++)
	{
		if(*value == SList[i]) 
			return (i+1);
	}
	return 0;
}

template <class Type>
void SequenceList<Type>::Update(const int index , Type * value)
{
	//改
	if(index > MaxSize || index < 0)
	{
		 throw std::out_of_range("out of bounds!!");
	}

	if(index >Len) return;

	SList[index] = *value;
}

template <class Type>
void SequenceList<Type>::OutPut(std::ostream & out) const
{
	//测试用
	for (int i = 0; i < Len; i++)
	{
		out << SList[i] << " ";
	}
}

template <class Type>
SequenceList<Type>::~SequenceList()
{
	//析构函数 不要忘了delete 还有[]
	delete[] SList;
	SList = NULL;
}

template <class Type>
int SequenceList<Type>::Length()
{
	//求长度
	return Len;
}

template <class Type>
bool SequenceList<Type>::IsEmpty()
{
	//判断是否为空
	return (0 == Len);
}
#endif


源文件.cpp

//模板是在真正实例化的时候才真正编译生成代码的
//为容易使用几乎总是在头文件中放置全部的模板声明和定义的
#include <iostream>

#include "SequenceList.h"

using namespace std;

int * array = new int[20];

void Init(int * (&arr) , int len)
{
	for(int i = 0 ; i < len ; i ++)
	{
		arr[i] = 2*i;
	}
}

int main()
{
	SequenceList<int> sList(20);
	cout << "Length = " << sList.Length() << endl;
	cout << "is empty ? " << sList.IsEmpty() <<endl;

	Init(array , 6);

	sList.Insert(0 ,&array[0]);
	sList.Insert(1 ,&array[1]);
	sList.Insert(2 , &array[2]);
	sList.Insert(3 , &array[3]);
	sList.Insert(4 , &array[4]);
	sList.OutPut(cout);

	int value = 4;

	cout << "index:" << sList.GetIndex(&value) << endl;

	value = 4;
	sList.Delete( 2 , &value);
	sList.OutPut(cout);
	cout << endl;
	value = 100;
	sList.Update(0 , &value);
	sList.OutPut(cout);
	cout << endl;
	system("pause");
}
见笑了!祝莘莘学子高考顺利.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值