顺序表(C++语言实现)

<span style="font-size:24px;">顺序表(C++语言实现)
</span>
<span style="font-size:24px;">含有头插尾插、头删尾删、插入、修改、删除、查找等功能</span>

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
using namespace std;

typedef int DataType;

typedef struct FindRet
{
	bool isFind;
	size_t index;
}FindRet;

class SeqList
{
public:
	SeqList()//构造函数
		:_array(NULL)
		, _size(0)
		, _capacity(0)
	{}

	SeqList(const SeqList& sList)//拷贝构造函数
	{
		_array = new DataType[sList._capacity];
		memcpy(_array, sList._array, sizeof(DataType)*sList._size);
	}
	SeqList& operator=(const SeqList& sList)//赋值运算符的重载
	{

	}

	~SeqList()
	{
		if (_array)
		{
			delete[] _array;
		}
		_size = 0;
		_capacity = 0;
	}
public:
	void PushBack(const DataType& x)
	{
		_CheckCapacity();
		_array[_size++] = x;
	}

	void PopBack()
	{
		if (_size)
			--_size;
	}

	void PushFront(const DataType& x)
	{
		_CheckCapacity();
		size_t i = _size;
		for (; i > 0; i--)
		{
			_array[i] = _array[i-1];
		}
		_array[0] = x;
		_size++;
	}
	void PopFront()
	{
		if (_size)
		{
			int i = 0;
			for (; i < _size; i++)
			{
				_array[i] = _array[i + 1];
			}
			--_size;
		}
	}
	void DisPlay()
	{
		size_t i = 0;
		for ( ; i < _size; ++i)
		{
			cout << _array[i] << "->" ;
		}
		cout << endl;
	}
	
	void Insert(size_t index, const DataType& x )
	{
		_CheckCapacity();
		size_t i = _size;
		for (; i > index; i--)
		{
			_array[i] = _array[i - 1];
		}
		_array[index] = x;
		_size++;
	}

	void Modified(size_t index, const DataType& x)
	{
		_array[index] = x;
	}

	void Remove(size_t index)
	{
		for (; index < _size; index++)
		{
			_array[index] = _array[index + 1];
		}
		--_size;
	}

	FindRet Find(SeqList* pSeq, const DataType& x, size_t index)
	{
		FindRet ret;
		for (; index < _size; index++)
		{
			if (x == _array[index])
			{
				ret.isFind = true;
				ret.index = index;
				cout << "is Find." << endl;
				return ret;
			}
			ret.isFind = false;
			cout << "isn't Find." << endl;
			return ret;
		}
	}

private:
	DataType* _array;
	size_t _size;
	size_t _capacity;

private:
	void _CheckCapacity()
	{
		if (_size == _capacity)
		{
			_capacity = 2 * _capacity + 3;
			DataType* tmp = new DataType[_capacity];
			memcpy(tmp,_array,sizeof(DataType)*_size);
			delete[] _array;
			_array = tmp;
		}
	}
};

void Test1()
{
	SeqList s1;
	s1.PushBack(1);
	s1.PushBack(2);
	s1.PushBack(3);
	s1.PushBack(4);
	s1.PushBack(5);
	s1.PushBack(6);
	s1.DisPlay();

	s1.PopBack();
	s1.PopBack();
	s1.PopBack();
	s1.DisPlay();

}

void Test2()
{
	SeqList s2;
	s2.PushFront(1);
	s2.PushFront(2);
	s2.PushFront(3);
	s2.PushFront(4);
	s2.PushFront(5);
	s2.PushFront(6);

	s2.DisPlay();

	s2.PopFront();
	s2.PopFront();
	s2.PopFront();
	s2.DisPlay();

}

void Test3()
{
	SeqList s3;
	s3.PushBack(1);
	s3.PushBack(2);
	s3.PushBack(3);
	s3.PushBack(4);
	s3.PushBack(5);
	s3.PushBack(6);
	s3.DisPlay();

	s3.Modified(2, 8);
	s3.DisPlay();

	s3.Find(&s3, 1, 0);

	s3.Remove(1);
	s3.DisPlay();

	s3.Insert(5, 9);
	s3.DisPlay();
}
int main()
{
	Test1();
	Test2();
	Test3();
	system("pause");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值