C++数据结构---顺序结构

本文探讨了C++中数据结构的顺序结构,包括相关头文件、实现代码和使用示例。通过阅读,你可以了解到如何在C++中创建和操作顺序存储的数据结构。

顺序结构.h

#pragma once
#include<iostream>

using namespace std;

class Myvector
{
public:
	Myvector();

	//打印vector中的元素
	void print();

	//尾插法
	void m_push_back(int val);

	//尾删法
	void m_pop_back();

	//头插法
	void m_push_front(int val);

	//头删法
	void m_pop_front();

	//在某个位置插入(下标法)
	void m_insert(int pos, int val);

	//在某个位置删除(下标法)
	void m_erase(int pos);

	//清理vector容器
	void clear();

	//查找
	int m_find(int val);




	int * first;//存放第一个元素的地址
	int capacity;//容器的容量
	int size;//容器中元素的个数
};
Myvector::Myvector()
{
	first = new int[4];
	capacity = 4;
	size = 0;
}

//拷贝函数
void m_copy(int* f, int* n,int size)
{
	for (; size > 0; n++,f++, size--)
	{
		*n = *f;
	}
}

//尾插法
void Myvector::m_push_back(int val)
{
	if (this->size == this->capacity)
	{
		int* newvector = new int[this->capacity * 2];//创建一个新的数组是原数组的2倍大小
		m_copy(first, newvector, this->size);		//将原数组拷贝到新数组上
		delete[]first;
		this->first = newvector;
		*(first + size) = val;//插入数值
		//更新状态
		this->capacity *= 2;
		this->size++;
	}
	else
	{
		*(first + size) = val;
		this->size++;
	}
}

//尾删法
void Myvector::m_pop_back()
{
	if (this->size == 0)
	{
		return;
	}
	else
	{
		this->size--;//更新大小
	}
}

//打印vector中的元素
void Myvector::print()
{
	for (int i = 0; i < this->size; i++)
	{
		cout << *(first + i) << "\t";
	}
	cout << endl;
}

//清理vector容器
void Myvector::clear()
{
	delete[]first;
	this->first = new int[4];
	this->capacity = 4;
	this->size = 0;
}

//头插法
void Myvector::m_push_front(int val)
{
	if (this->size == this->capacity)//如果容器满了
	{
		int* newvector = new int[this->capacity * 2];//创建一个新的容器,大小是原容器的2倍
		m_copy(this->first, newvector, this->size);	//将原容器的函数拷贝到新容器中
		delete[] first;//释放原容器
		this->first = newvector;//将指针指向新容器的首地址

		for (int i = size-1; i >=0; i--)
		{
			*(first + i + 1) = *(first + i);
		}
		*first = val;
		this->size++;
	}
	else
	{
		for (int i = size - 1; i >= 0; i--)
		{
			*(first + i + 1) = *(first + i);
		}
		*first = val;
		this->size++;
	}
}

//头删法
void Myvector::m_pop_front()
{
	if (this->size == 0)
	{
		return;
	}
	else
	{
		this->first = first + 1;
		this->size--;
	}
}

//在某个位置插入(下标法)
void Myvector::m_insert(int pos, int val)
{
	if (pos > this->size || pos < 0)
	{
		return;
	}
	if (this->size == this->capacity)
	{
		int* newvector = new int[this->capacity * 2];
		m_copy(this->first, newvector, this->size);
		delete[] first;
		this->first = newvector;

		int end = this->size - 1;
		while (end >= pos)
		{
			*(this->first + end + 1) = *(this->first + end);
			end--;
		}
		*(this->first + pos) = val;
		this->size++;
	}
	else
	{
		int end = this->size - 1;
		while (end >= pos)
		{
			*(this->first + end + 1) = *(this->first + end);
			end--;
		}
		*(this->first + pos) = val;
		this->size++;
	}
}

//在某个位置删除(下标法)
void Myvector::m_erase(int pos)
{
	if (this->size == 0)
	{
		return;
	}
	else
	{
		int cur = pos;
		while (cur < this->size-1)
		{
			*(this->first + cur) = *(this->first + cur + 1);
			cur++;
		}
		this->size--;
	}
}

//查找
int Myvector::m_find(int val)
{
	for (int i = 0; i < this->size; i++)
	{
		if (*(this->first + i) == val)
		{
			return i;
		}
	}
	return -1;
}

顺序结构.cpp

int main()
{
	Myvector m;
	m.m_push_back(10);
	m.m_push_back(20);
	m.m_push_back(30);
	m.m_push_back(40);
	m.m_push_back(50);
	m.m_pop_back();
	m.m_push_front(1);
	m.m_push_front(2);
	m.m_pop_front();
	m.m_insert(2, 5);
	m.m_insert(2, 11);
	m.m_erase(2);
	cout << m.m_find(11) << endl;

	m.print();
	return 0;
}

希望大家赞一个,有错误请指正!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值