順序表的實現:
順序表的增刪改查
#pragma once
#include <iostream>
using namespace std;
/*
插入元素的時候
1、線性表需要判斷表是否滿
2、插入的位置是否有效
刪除元素:
1、表是否爲空
2、刪除的元素是否有效
*/
template <typename T>
class arrList
{
public:
arrList(const int size);
~arrList();
void clearArr();
int lengthArr();
bool fullArr(); //判斷是否滿
bool apendArr(const T value); //尾部添加元素
bool insertArr(const int p, const T value); //插入元素
bool deleteArr(const int p); //刪除元素
bool setValueArr(const int p, const T value); //修改某個位置的元素
bool getValueArr(const int p, T& value); //獲取某個位置的元素
bool getPosArr(int &p, const T value); //查找是否有這個值在表中
void showArr();
private:
int _curLen; //表實例當前長度
int _maxLen; //表實例最大長度 有一個元素0的時候長度爲1
T *_aList; //存儲表實例
int _position; //當前處理位置 處理位置從0開始
};
template <typename T>
arrList<T>::arrList(const int size):_curLen(0),_maxLen(size),_position(0)
{
cout << "arrList(const int size)" << endl;
_aList = new T[size];
cout << "type(_aList) :" << typeid(_aList).name() << endl;
cout << "_maxLen :" << _maxLen << endl;
cout << sizeof(_aList)/sizeof(int) << endl;
}
template <typename T>
arrList<T>::~arrList()
{
cout << "~arrList()" << endl;
delete[] _aList;
_aList = NULL;
}
template <typename T>
void arrList<T>::clearArr() //將順序表中的內容清零,成爲空表
{
cout << "clearArr()" << endl;
delete[] _aList;
_aList = new T[_maxLen];
_curLen = _position = 0;
}
template <typename T>
int arrList<T>::lengthArr() //放回當前的實際長度
{
cout << "lengthArr()" << endl;
return _curLen;
}
template <typename T>
bool arrList<T>::fullArr()
{
cout << "fullArr()" << endl;
return _curLen == _maxLen;
}
template <typename T>
bool arrList<T>::apendArr(const T value) //在表尾添加元素V
{
cout << "apendArr(const T value)" << endl;
if (_curLen >= _maxLen) {
cout << "This list is overflow" << endl;
return false;
}
_aList[_curLen] = value;
_curLen++;
return true;
}
template <typename T>
bool arrList<T>::insertArr(const int p, const T value) //插入元素
{
int i;
cout << "insertArr(const int p, const T value)" << endl;
if (_curLen >= _maxLen) {
cout << "This list is overflow" << endl;
return false;
}
if (p < 0 || p >= _curLen) {
cout << "insert postion is illegal" << endl;
return false;
}
for (i = _curLen; i > p; i--)
{
_aList[i] = _aList[i- 1];
}
_aList[p] = value;
_curLen++;
return true;
}
template <typename T>
bool arrList<T>::deleteArr(const int p) //刪除位置p上的元素
{
int i;
cout << "deleteArr(const int p)" << endl;
cout << "__curLen : " << _curLen << endl;
if (_curLen <= 0) {
cout << "no element to delete" << endl;
return false;
}
if (p < 0 || p > _curLen) {
cout << "delate postion is illegal" << endl;
return false;
}
cout << "_position : " << _position << endl;
for (i = p; i < _curLen - 1; i++) {
cout << "_position : " << _position << endl;
_aList[i] = _aList[i + 1];
}
_curLen--;
cout << "__curLen : " << _curLen << endl;
return true;
}
template <typename T>
bool arrList<T>::setValueArr(const int p, const T value) //設置元素值
{
cout << "setValueArr(const int p, const T value)" << endl;
if (p < 0 || p > _curLen -1) {
cout << "setValueArr postion is illegal" << endl;
return false;
}
_aList[p] = value;
return true;
}
template <typename T>
bool arrList<T>::getValueArr(const int p, T& value) //返回元素
{
cout << "getValueArr(const int p, T& value)" << endl;
if (_curLen <= 0) {
cout << "no element to delete" << endl;
return false;
}
if (p < 0 || p > _curLen) {
cout << "insert postion is illegal" << endl;
return false;
}
value = _aList[p];
return true;
}
template <typename T>
bool arrList<T>::getPosArr(int &p, const T value) //查找元素
{
cout << "getPosArr(int &p, const T value)" << endl;
if (_curLen <= 0) {
cout << "no element to find" << endl;
return false;
}
for (_position = 0; _position < _curLen; _position++)
{
if (value == _aList[_position]) {
p = _position;
return true;
}
}
return false;
}
template <typename T>
void arrList<T>::showArr()
{
cout << "showArr()" << endl;
for (int i = 0; i < _curLen; i++)
{
cout << _aList[i] << " ";
}
#if 0
int i = 0;
while (i < _curLen) {
cout << _aList[i] << " ";
}
#endif
cout << endl;
}
#if 0
template <>
bool arrList<char *>::apendArr(char * const value) //在表尾添加元素V
{
cout << "apendArr(const T value)" << endl;
if (_curLen >= _maxLen) {
cout << "This list is overflow" << endl;
return false;
}
_aList[_curLen] = value;
_curLen++;
return true;
}
#endif
#include <iostream>
#include "arrList.h"
using namespace std;
int main()
{
arrList<int> array1(10);
for (int i = 0; i < 10; i++)
{
array1.apendArr(rand()%100 +1);
}
array1.showArr();
arrList<int> array2(10);
for (int i = 0; i < 10; i++)
{
array2.apendArr(i);
}
array2.showArr();
array2.deleteArr(3);
array2.deleteArr(5);
array2.showArr();
array2.insertArr(3,12);
array2.setValueArr(0, 100);
array2.showArr();
int p;
array2.getPosArr(p,1);
cout << "getPosArr postion : " << p << endl;
int tp;
array2.getValueArr(0, tp);
cout << "getValueArr : " << tp << endl;
array2.clearArr();
array2.apendArr(111);
array2.showArr();
cout << "array2.lengthArr : " << array2.lengthArr() << endl << "array2.fullArr : " << array2.fullArr() << endl;
double tmp = 0.0;
arrList<double> array3(10);
for (int i = 0; i < 10; i++)
{
tmp = tmp + 1 / 3.0;
array3.apendArr(tmp);
}
array3.showArr();
char *strtmp = new char(10);
memcpy(strtmp, "hi", 3);
arrList<char *> array4(10);
for (int i = 0; i < 10; i++)
{
array4.apendArr(strtmp);
}
array4.showArr();
}