C++实现顺序表

本文详细介绍了C++中的SeqList模板类,它是一个动态顺序表,支持后添加、头部插入、删除、查找和修改等操作,以及动态扩容功能。

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

SeqList.h

#pragma once
#include <iostream>
#include <cassert>

#define N 200
typedef int SLDatatype;

using namespace std;
/*
顺序表:在计算机内存中以数组形式保存的线性表,采用顺序存储结构(数据元素物理存储地址相邻)
*/

//动态顺序表
template<typename t>
class SeqList {
	t* ptr; //动态数组指针
	t data;//传入数据
	int size;//数据个数
	int capacity;//容量-空间大小(可容纳的数据个数)
public:
	SeqList();
	void PushBack(t data);//后添加
	void SLPrint();//打印
	void SLDelete();//销毁
	void SLPushFront(t Data);//头插
	void SLPopFront();//头删
	void SLInsert(t data, int pos);//插值
	void SLErase(int pos);//删除某个值
	int SLFind(t data);//查找某个值位置
	void Modify(t data, int pos);//修改某个位置的值

private:
	void CheckCapacity(); //扩容
	


};


template<typename t>
inline SeqList<t>::SeqList()
{
	this->ptr = nullptr;
	this->size = this->capacity = 0;

}

template<typename t>
void SeqList<t>::CheckCapacity()
{
	if (this->size == this->capacity) 
	{
		this->capacity = this->capacity + 1;
		t* tmp = (t*)realloc(this->ptr, this->capacity * sizeof(t));
		if (tmp == NULL)
		{
			perror("relloc:");
			exit(-1);
		}
		this->ptr = tmp;
	}
}

template<typename t>
void SeqList<t>::PushBack(t data)
{
	this->CheckCapacity();
	this->ptr[this->size] = data;
	this->size++;
}

template<typename t>
void SeqList<t>::SLPrint()
{
	cout << "seqlist: ";
	for (int i = 0; i < this->size; i++)
	{
		cout << " " << this->ptr[i];
	}
	cout << endl;
}

template<typename t>
void SeqList<t>::SLDelete()
{
	if (this->ptr) 
	{
		free(this->ptr);
		this->ptr = NULL;
		this->size = this->capacity = 0;
		cout << "delete success" << endl;
	}
	else
	{
		cout << "delete unfinished...." << endl;
	}
	
}

template<typename t>
void SeqList<t>::SLPushFront(t data)
{
	this->CheckCapacity();
	int end = this->size - 1;
	while (end >= 0) {
		this->ptr[end + 1] = this->ptr[end];
		end--;
	}
	this->ptr[0] = data;
	this->size++;
}

template<typename t>
void SeqList<t>::SLPopFront()
{
	assert(this->size);
	int begin = 1;
	while (begin < this->size)
	{
		this->ptr[begin - 1] = this->ptr[begin];
		begin++;
	}
	this->size--;
}

template<typename t>
void SeqList<t>::SLInsert(t data, int pos)
{
	assert(pos >= 0 && pos <= this->size);
	CheckCapacity();
	int end = this->size - 1;
	while (end >= pos)
	{
		this->ptr[end + 1] = this->ptr[end];
		end--;
	}
	this->ptr[pos] = data;
	this->size++;
}

template<typename t>
void SeqList<t>::SLErase(int pos)
{
	assert(pos >= 0 && pos <= this->size);
	int begin = pos + 1;
	while (begin < this->size)
	{
		this->ptr[begin - 1] = this->ptr[begin];
		begin++;
	}
	this->size--;
}

template<typename t>
int SeqList<t>::SLFind(t data)
{
	for (int i = 0; i <= this->size; i++)
	{
		if (this->ptr[i] == data)
			return i;
	}
	return -1;
}

template<typename t>
void SeqList<t>::Modify(t data, int pos)
{
	assert(pos >= 0 && pos < this->size);
	this->ptr[pos] = x;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值