博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线性表——数组描述(Qt5.1测试代码 for windows)
阅读量:7100 次
发布时间:2019-06-28

本文共 10844 字,大约阅读时间需要 36 分钟。

hot3.png


主函数:

// test the class arrayList that uses STL algorithms#include
#include "linearList.h"#include "arrayList.h"using namespace std;int main(){ // test constructor linearList
*x = new arrayList
(20); arrayList
y(2), z; // test capacity cout << "Capacity of x, y and z = " << ((arrayList
*) x)->capacity() << ", " << y.capacity() << ", " << z.capacity() << endl; // test size cout << "Initial size of x, y, and z = " << x->size() << ", " << y.size() << ", " << z.size() << endl; // test empty if (x->empty()) cout << "x is empty" << endl; else cout << "x is not empty" << endl; if (y.empty()) cout << "y is empty" << endl; else cout << "y is not empty" << endl; // test insert y.insert(0, 2); y.insert(1, 6); y.insert(0, 1); y.insert(2, 4); y.insert(3, 5); y.insert(2, 3); cout << "Inserted 6 integers, list y should be 1 2 3 4 5 6" << endl; cout << "Size of y = " << y.size() << endl; cout << "Capacity of y = " << y.capacity() << endl; if (y.empty()) cout << "y is empty" << endl; else cout << "y is not empty" << endl; y.output(cout); cout << endl << "Testing overloaded <<" << endl; cout << y << endl; // test indexOf int index = y.indexOf(4); if (index < 0) cout << "4 not found" << endl; else cout << "The index of 4 is " << index << endl; index = y.indexOf(7); if (index < 0) cout << "7 not found" << endl; else cout << "The index of 7 is " << index << endl; // test get cout << "Element with index 0 is " << y.get(0) << endl; cout << "Element with index 3 is " << y.get(3) << endl; // test erase y.erase(1); cout << "Element 1 erased" << endl; cout << "The list is " << y << endl; y.erase(2); cout << "Element 2 erased" << endl; cout << "The list is " << y << endl; cout << "Size of y = " << y.size() << endl; cout << "Capacity of y = " << y.capacity() << endl; if (y.empty()) cout << "y is empty" << endl; else cout << "y is not empty" << endl; try {y.insert(-3, 0);} catch (illegalIndex e) { cout << "Illegal index exception" << endl; cout << "Insert index must be between 0 and list size" << endl; e.outputMessage(); } // test copy constructor arrayList
w(y); y.erase(0); y.erase(0); cout << "w should be old y, new y has first 2 elements removed" << endl; cout << "w is " << w << endl; cout << "y is " << y << endl; // a few more inserts, just for fun y.insert(0,4); y.insert(0,5); y.insert(0,6); y.insert(0,7); cout << "y is " << y << endl; return 0;}

数据结构中使用的类的定义,包括类中各种函数的具体实现,都保存在头文件中,具体代码如下:

arrayList.h

#ifndef ARRAYLIST_H#define ARRAYLIST_H// array implementation of a linear list// derives from abstract class linearList just to make sure// all methods of the ADT are implemented// USES STL ALGORITHMS TO SIMPLIFY CODE#include
#include
#include
#include
#include
#include "linearList.h"#include "myExceptions.h"#include "changeLength1D.h"using namespace std;template
class arrayList : public linearList
{ public: // constructor, copy constructor and destructor arrayList(int initialCapacity = 10); arrayList(const arrayList
&); ~arrayList() {delete [] element;} // ADT methods bool empty() const {return listSize == 0;} int size() const {return listSize;} T& get(int theIndex) const; int indexOf(const T& theElement) const; void erase(int theIndex); void insert(int theIndex, const T& theElement); void output(ostream& out) const; // additional method int capacity() const {return arrayLength;} protected: void checkIndex(int theIndex) const; // throw illegalIndex if theIndex invalid T* element; // 1D array to hold list elements int arrayLength; // capacity of the 1D array int listSize; // number of elements in list};template
arrayList
::arrayList(int initialCapacity){// Constructor. if (initialCapacity < 1) {ostringstream s; s << "Initial capacity = " << initialCapacity << " Must be > 0"; throw illegalParameterValue(s.str()); } arrayLength = initialCapacity; element = new T[arrayLength]; listSize = 0;}template
arrayList
::arrayList(const arrayList
& theList){// Copy constructor. arrayLength = theList.arrayLength; listSize = theList.listSize; element = new T[arrayLength]; copy(theList.element, theList.element + listSize, element);}template
void arrayList
::checkIndex(int theIndex) const{// Verify that theIndex is between 0 and listSize - 1. if (theIndex < 0 || theIndex >= listSize) {ostringstream s; s << "index = " << theIndex << " size = " << listSize; throw illegalIndex(s.str()); }}template
T& arrayList
::get(int theIndex) const{// Return element whose index is theIndex. // Throw illegalIndex exception if no such element. checkIndex(theIndex); return element[theIndex];}template
int arrayList
::indexOf(const T& theElement) const{// Return index of first occurrence of theElement. // Return -1 if theElement not in list. // search for theElement int theIndex = (int) (find(element, element + listSize, theElement) - element); // check if theElement was found if (theIndex == listSize) // not found return -1; else return theIndex; }template
void arrayList
::erase(int theIndex){// Delete the element whose index is theIndex. // Throw illegalIndex exception if no such element. checkIndex(theIndex); // valid index, shift elements with higher index copy(element + theIndex + 1, element + listSize, element + theIndex); element[--listSize].~T(); // invoke destructor}template
void arrayList
::insert(int theIndex, const T& theElement){// Insert theElement so that its index is theIndex. if (theIndex < 0 || theIndex > listSize) {// invalid index ostringstream s; s << "index = " << theIndex << " size = " << listSize; throw illegalIndex(s.str()); } // valid index, make sure we have space if (listSize == arrayLength) {// no space, double capacity changeLength1D(element, arrayLength, 2 * arrayLength); arrayLength *= 2; } // shift elements right one position copy_backward(element + theIndex, element + listSize, element + listSize + 1); element[theIndex] = theElement; listSize++;}template
void arrayList
::output(ostream& out) const{// Put the list into the stream out. copy(element, element + listSize, ostream_iterator
(cout, " "));}// overload <

changeLength1d.h

#ifndef CHANGELENGTH1D_H#define CHANGELENGTH1D_H#include "myExceptions.h"using namespace std;template
void changeLength1D(T*& a, int oldLength, int newLength){ if (newLength < 0) throw illegalParameterValue("new length must be >= 0"); T* temp = new T[newLength]; // new array int number = min(oldLength, newLength); // number to copy copy(a, a + number, temp); delete [] a; // deallocate old memory a = temp;}#endif // CHANGELENGTH1D_H

linearList.h

#ifndef LINEARLIST_H#define LINEARLIST_H// LINEARLIST_H// abstract class linearList// abstract data type specification for linear list data structure// all methods are pure virtual functions#include 
using namespace std;template
class linearList{ public: virtual ~linearList() {}; virtual bool empty() const = 0; // return true iff list is empty virtual int size() const = 0; // return number of elements in list virtual T& get(int theIndex) const = 0; // return element whose index is theIndex virtual int indexOf(const T& theElement) const = 0; // return index of first occurence of theElement virtual void erase(int theIndex) = 0; // remove the element whose index is theIndex virtual void insert(int theIndex, const T& theElement) = 0; // insert theElement so that its index is theIndex virtual void output(ostream& out) const = 0; // insert list into stream out};#endi

myexceptions.h

#ifndef MYEXCEPTIONS_H#define MYEXCEPTIONS_H#include 
using namespace std;// illegal parameter valueclass illegalParameterValue{ public: illegalParameterValue(string theMessage = "Illegal parameter value") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// illegal input dataclass illegalInputData{ public: illegalInputData(string theMessage = "Illegal data input") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// illegal indexclass illegalIndex{ public: illegalIndex(string theMessage = "Illegal index") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// matrix index out of boundsclass matrixIndexOutOfBounds{ public: matrixIndexOutOfBounds (string theMessage = "Matrix index out of bounds") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// matrix size mismatchclass matrixSizeMismatch{ public: matrixSizeMismatch(string theMessage = "The size of the two matrics doesn't match") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// stack is emptyclass stackEmpty{ public: stackEmpty(string theMessage = "Invalid operation on empty stack") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// queue is emptyclass queueEmpty{ public: queueEmpty(string theMessage = "Invalid operation on empty queue") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// hash table is fullclass hashTableFull{ public: hashTableFull(string theMessage = "The hash table is full") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// edge weight undefinedclass undefinedEdgeWeight{ public: undefinedEdgeWeight(string theMessage = "No edge weights defined") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};// method undefinedclass undefinedMethod{ public: undefinedMethod(string theMessage = "This method is undefined") {message = theMessage;} void outputMessage() {cout << message << endl;} private: string message;};#endif // MYEXCEPTIONS_H

转载于:https://my.oschina.net/u/1771419/blog/1791576

你可能感兴趣的文章
上手并过渡到PHP7(5)——轻量级“集合”迭代器-Generator
查看>>
git的配置
查看>>
python2.0_s12_day19_前端结合后端展示客户咨询纪录
查看>>
angular中$location读取url信息
查看>>
POJ1837 Balance[分组背包]
查看>>
防火墙/IDS测试工具Ftester
查看>>
iOS WebSocket
查看>>
Java多线程 -- wait() 和 notify() 使用入门
查看>>
React@16.3 全新的Context API进阶教程
查看>>
区块链开发教程系列【加精】
查看>>
dubbo源码解析(四十一)集群——Mock
查看>>
前端面试问题汇总
查看>>
4.java数组
查看>>
MySQL数据类型优化
查看>>
蚂蚁金服核心技术:百亿特征实时推荐算法揭秘 ...
查看>>
阿里云智能技术战略架构师陈绪:透视2019云计算酣战 ...
查看>>
深度学习要多深,才能读懂人话?|阿里小蜜前沿探索 ...
查看>>
好程序员分享如何看待CSS中BEM的命名方式?
查看>>
国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
查看>>
从零开始学习JAVA多线程(二)
查看>>