class_SeqList.

#include <iostream>
using namespace std;

typedef int DataType;

class SeqList
{
public:
	SeqList()
		:_array(NULL)
		, _size(0)
		, _capicity(0)
	{}

	~SeqList()
	{
		if (_array)
		{
			delete[] _array;
		}

		_size = 0;
		_capicity = 0;
	}

	// 使用优化的写法
	SeqList(const SeqList& sList)
	{
		_array = new DataType[sList._capicity];
		memcpy(_array, sList._array, sizeof(DataType)*sList._size);
		_size = sList._size;
		_capicity = sList._capicity;
	}

	SeqList& operator=(const SeqList& sList)
	{
		delete[] _array;
		_array = new DataType[sList._capicity];
		memcpy(_array, sList._array, sizeof(DataType)*sList._size);
		_size = sList._size;
		_capicity = sList._capicity;
		return *this;
	}
public:
	void Print()
	{
		for (size_t i = 0; i < _size; ++i)
		{
			cout << _array[i] << " ";
		}
		cout << endl;
	}

	void PushBack(const DataType& x)
	{
		_CheckCapicity();

		_array[_size++] = x;
	}

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

	void PushFront(const DataType& x)
	{
		_CheckCapicity();
		for (int i = _size; i > 0; i--)
		{
			_array[i] = _array[i - 1];
		}
		_array[0] = x;
		_size++;
	}

	void PopFront()
	{
		if (_size)
		{
			for (int i = 0; i < _size - 1; i++)
			{
				_array[i] = _array[i + 1];
			}

			--_size;
		}
	}

	// 实现上面的函数
	void Insert(size_t index, const DataType& x)
	{
		if (index >= 0 && index < _size)
		{
			for (int i = _size; i > index; i--)
			{
				_array[i] = _array[i - 1];
			}
			_array[index] = x;
		}
		++_size;
	}
	void Modified(size_t index, const DataType& x)
	{
		if (index >= 0 && index < _size)
		{
			_array[index] = x;
		}
	}
	void Remove(size_t index)
	{
		if (index >= 0 && index < _size)
		{
			for (int i = index; i < _size - 1; i++)
			{
				_array[i] = _array[i + 1];
			}
		}
		--_size;
	}
	bool Find(const DataType& x)
	{
		if (_size == 0)
		{
			return NULL;
		}
		else if (_size == 1)
		{
			if (_array[0] == x)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			for (int i = 0; i < _size; i++)
			{
				if (_array[i] == x)
				{
					return true;
				}
			}
			return false;
		}
	}

private:
	void _CheckCapicity()
	{
		if (_size == _capicity)
		{
			_capicity = 2 * _capicity + 3;
			DataType* tmp = new DataType[_capicity];
			memcpy(tmp, _array, sizeof(DataType)*_size);

			delete[] _array;
			_array = tmp;
		}
	}

private:
	DataType* _array;
	size_t _size;
	size_t _capicity;
};

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

	SeqList s2(s1);
	s2.Print();

	SeqList s3;
	s3 = s1;
	s3.Print();
}
int main()
{
	Test1();

	SeqList s;
	int i = 0;
	size_t index;
	while (1)
	{
		cout << "请选择:" << endl;
		cout << "1.尾插	2.尾删" << endl;
		cout << "3.头插	4.头删" << endl;
		cout << "5.插入	6.修改" << endl;
		cout << "7.删除 8.打印" << endl;
		cout << "9.查找       " << endl;

		cin >> i;
		int x = 0;
		switch (i)
		{
		case 1:
			cout << "请依次输入数据,以-1结束" << endl;
			do{
				cin >> x;
				if (x != -1)
					s.PushBack(x);
			} while (x != -1);
			break;
		case 2:
			s.PopBack();
			break;
		case 3:
			cout << "请输入需要插入的数:" << endl;
			cin >> x;
			s.PushFront(x);
			break;
		case 4:
			s.PopFront();
			break;
		case 5:
			cout << "请输入要插入的位置下标:"<<"  ";
			cin >> index;
			cout << "请输入要插入的数:" << "  ";
			cin >> x;
			s.Insert(index, x);
			break;
		case 6:
			cout << "请输入要修改数的下标以及要改为的数:" << " ";
			cin >> index;
			cin >> x;
			s.Modified(index, x);
			break;
		case 7:
			cout << "请输入要删除数的下标:" << " ";
			cin >> index;
			s.Remove(index);
			break;

		case 8:
			s.Print();
			break;
		case 9:
			cout << "请输入要查找的数:" << " ";
			cin >> x;
			cout << s.Find(x) << endl;
			break;
		default:
			cout << "输入错误,请重新输入" << endl;
			break;
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值