数据结构C++实现代码-顺序表

参考:https://blog.csdn.net/ebowtang/article/details/43094041

//seqList.h//
//包含顺序表中的声明// #include<iostream>
template<typename DataType>
class SeqList
{
public:
SeqList(int size = defaultSize)
{
if (size > )
{
maxSize = size;
length = ;
elements = new DataType[maxSize];
for (int i = ; i < maxSize; i++)
{
elements[i] = NULL;
} }
else
{
cout << "Error Length of SeqList!" << endl; }
}
~SeqList()
{
//delete[] elements;
}
bool InsertElement(DataType data);
DataType GetElement(int location);
bool DelElement(int location);
bool ChangeElement(int location, DataType data);
bool PrintList();
int FindElement(DataType data);
bool isEmpty(SeqList L);
bool InitList(int nLen);
void ClearList(SeqList *L);
void DestroyList();
void ConverseList();
int getLength()
{
return length;
}
private:
static const int defaultSize = ;
DataType *elements;
int maxSize;
int length;
};
//seqList.cpp//
//包含顺序表中主要函数功能的具体实现//
#include<iostream>
#include"seqList.h"
template <typename DataType>
bool SeqList<DataType>::InsertElement(DataType data)
{
int curIndex = length;
if (length >= maxSize)
{
return false;
}
else
{
elements[curIndex] = data;
length++;
return true;
}
} template <typename DataType>
bool SeqList<DataType>::InitList(int nLen)
{
DataType nchar = 'A';
for (int i = ; i < nLen; i++)
{
InsertElement(nchar++);
}
return true;
}
template <typename DataType>
DataType SeqList<DataType>::GetElement(int location)
{
if (location< || location>length)
{
std::cout << "参数无效" << std::endl;
return ;
}
else
{
return elements[location];
}
}
template <typename DataType>
bool SeqList<DataType>::DelElement(int location)
{
if (location< || location >length)
{
std::cout << "参数无效" << std::endl;
return false;
}
else
{
int j = ;
for (int i = location; i < length; i++)
{
elements[location + j - ] = elements[location + j];
j++;
}
length--;
return true;
}
}
template <typename DataType>
bool SeqList<DataType>::ChangeElement(int location, DataType data)
{
if (location< || location>length)
{
std::cout << "参数无效" << std::endl;
return false;
}
else
{
elements[location - ] = data;
return true;
}
} template <typename DataType>
bool SeqList<DataType>::PrintList()
{
for (int i = ; i < length; i++)
std::cout << GetElement(i) << " ";
std::cout<< endl;
return true;
}
template <typename DataType>
int SeqList<DataType>::FindElement(DataType data)
{
for (int i = ; i < length; i++)
{
if (elements[i] == data)
{
return i;
}
}
std::cout << "没有更改元素" << std::endl;
return ;
} template <typename DataType>
bool SeqList<DataType>::isEmpty(SeqList<DataType> L)
{
if (L.length == )
return true;
else
return false;
}
template <typename DataType>
void SeqList<DataType>::ClearList(SeqList *L)
{
for (int i = ; i < length; i++)
elements[i] = ;
L->length = ;
L->maxSize = ;
}
template <typename DataType>
void SeqList<DataType>::DestroyList()
{
length = ;
maxSize = ;
delete[] elements;
elements = NULL;
}
template <typename DataType>
void SeqList<DataType>::ConverseList()
{
for (int i = ; i < length / ; i++)
{
DataType temp;
temp = elements[i];
elements[i] = elements[length - - i];
elements[length - i - ] = temp;
}
}
//main.cpp//
#include "seqList.cpp"
#include "windows.h"
using namespace std;
typedef char Mytype;
int main(int argc, char* argv[])
{
int nLen = ;
cout << "请输入顺序表的长度: ";
cin >> nLen;
SeqList<Mytype>list(nLen);
list.InitList(nLen);
cout << "初始化后的内容为l(亲,萌妹纸自动帮您完成的哦!!!)" << endl;
list.PrintList();
int nPos = ;
cout << "你想删除的那个位置的元素(位置从1开始算起)";
cin >> nPos;
while (true)
{
if (nPos > list.getLength())
{
cout << "输入过大,重新输入(位置从1开始算起)" << endl;
cin >> nPos;
}
else
{
break;
}
}
list.DelElement(nPos);
list.PrintList();
cout << "现在顺序表的长度为: " << endl;
cout << list.getLength() << endl;
Mytype mchar = '';
int nPos1 = ;
char ans = 'n';
do
{
cout << "请输入您想改变的指定位置和相应元素(示例:“k 5”)";
cin >> mchar >> nPos1;
list.ChangeElement(nPos1, mchar);
cout << "继续修改?(Y/N)" << endl;
cin >> ans;
} while (ans == 'y' || ans == 'Y');
cout << "更改后的顺序表为: " << endl;
list.PrintList();
cout << "执行逆序: " << endl;
list.ConverseList();
list.PrintList();
Mytype bchar = '';
cout << "请输入您想查找的元素: ";
cin >> bchar;
int npos2 = list.FindElement(bchar);
cout << "您查找的元素的位置为:" << endl;
cout << npos2 << endl;
list.ClearList(&list);
if (list.isEmpty(list) == true)
{
cout << "顺序表已被清空" << endl; }
else
{
cout << "顺序表还原元素" << endl; }
cout << "5秒后执行销毁命令....................." << endl;
Sleep();
cout << "4秒后执行销毁命令....................." << endl;
Sleep();
cout << "3秒后执行销毁命令....................." << endl;
Sleep();
cout << "2秒后执行销毁命令....................." << endl;
Sleep();
cout << "1秒后执行销毁命令....................." << endl;
Sleep();
cout << "再见,谢谢....................." << endl;
list.DestroyList();
system("PAUSE");
return ;
}

代码运行结果图:

数据结构C++实现代码-顺序表

2018-04-2211:13:09

上一篇:阿里云一 第一篇:云服务器ECS


下一篇:动态执行文本vba代码