參考:https://blog.csdn.net/ebowtang/article/details/43094041
//seqList.h// //包含順序表中的聲明// #include<iostream> template<typename DataType> class SeqList { public: SeqList(int size = defaultSize) { if (size > 0) { maxSize = size; length = 0; elements = new DataType[maxSize]; for (int i = 0; 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 = 10; 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 = 0; i < nLen; i++) { InsertElement(nchar++); } return true; } template <typename DataType> DataType SeqList<DataType>::GetElement(int location) { if (location<0 || location>length) { std::cout << "參數無效" << std::endl; return 0; } else { return elements[location]; } } template <typename DataType> bool SeqList<DataType>::DelElement(int location) { if (location<0 || location >length) { std::cout << "參數無效" << std::endl; return false; } else { int j = 0; for (int i = location; i < length; i++) { elements[location + j - 1] = elements[location + j]; j++; } length--; return true; } } template <typename DataType> bool SeqList<DataType>::ChangeElement(int location, DataType data) { if (location<0 || location>length) { std::cout << "參數無效" << std::endl; return false; } else { elements[location - 1] = data; return true; } } template <typename DataType> bool SeqList<DataType>::PrintList() { for (int i = 0; i < length; i++) std::cout << GetElement(i) << " "; std::cout<< endl; return true; } template <typename DataType> int SeqList<DataType>::FindElement(DataType data) { for (int i = 0; i < length; i++) { if (elements[i] == data) { return i; } } std::cout << "沒有更改元素" << std::endl; return 0; } template <typename DataType> bool SeqList<DataType>::isEmpty(SeqList<DataType> L) { if (L.length == 0) return true; else return false; } template <typename DataType> void SeqList<DataType>::ClearList(SeqList *L) { for (int i = 0; i < length; i++) elements[i] = 0; L->length = 0; L->maxSize = 0; } template <typename DataType> void SeqList<DataType>::DestroyList() { length = 0; maxSize = 0; delete[] elements; elements = NULL; } template <typename DataType> void SeqList<DataType>::ConverseList() { for (int i = 0; i < length / 2; i++) { DataType temp; temp = elements[i]; elements[i] = elements[length - 1 - i]; elements[length - i - 1] = temp; } }
//main.cpp// #include "seqList.cpp" #include "windows.h" using namespace std; typedef char Mytype; int main(int argc, char* argv[]) { int nLen = 0; cout << "請輸入順序表的長度: "; cin >> nLen; SeqList<Mytype>list(nLen); list.InitList(nLen); cout << "初始化后的內容為l(親,萌妹紙自動幫您完成的哦!!!)" << endl; list.PrintList(); int nPos = 0; 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 = '0'; int nPos1 = 0; 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 = '0'; 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(1000); cout << "4秒后執行銷毀命令....................." << endl; Sleep(1000); cout << "3秒后執行銷毀命令....................." << endl; Sleep(1000); cout << "2秒后執行銷毀命令....................." << endl; Sleep(1000); cout << "1秒后執行銷毀命令....................." << endl; Sleep(1000); cout << "再見,謝謝....................." << endl; list.DestroyList(); system("PAUSE"); return 0; }
代碼運行結果圖:

2018-04-2211:13:09
