arrlist

来源:互联网 发布:中国广电网络投诉电话 编辑:程序博客网 时间:2024/06/04 19:32
/* 1. 插入删除等位置参数指数组下标 * 2. 判断条件有:表是否已满; 位置是否合法 */#ifndefLIST_H#defineLIST_H#include<iostream>using namespace std;template <class T>class arrList {private:T* aList;intmaxSize;intcurLen;intposition;public:arrList(const int size) {maxSize = size;aList = new T[maxSize];curLen = position = 0;cout<<"new empty list!"<<endl;}~arrList() {delete [] aList;}void print() {for (int i = 0; i < curLen; i++)cout<<aList[i]<<" ";cout<<endl;}void clear() {delete [] aList;curLen = position = 0;aList = new T[maxSize];cout<<"list has been cleared!"<<endl;}bool isEmpty() {if (curLen == 0) {cout<<"list is empty!"<<endl;return true;}return false;}int length() {return curLen;}bool getValue(const int p, T& value);bool setValue(const int p, const T value);bool getPos(int& p, const T value);bool append(const T value);bool insert(const int p, const T value);bool deletee(const int p);};template<class T>bool arrList<T>::getValue(const int p, T& value) {if (p < 0 || p >= curLen) {cout<<"illegal getvalue!"<<endl;return false;}value = aList[p];return true;}//only can set list[0] - list[curLen-1]template<class T>bool arrList<T>::setValue(const int p, const T value) {if (p < 0 || p >= curLen) {cout<<"illegal setvalue!"<<endl;return false;}aList[p] = value;return true;}// return array subscripttemplate<class T>bool arrList<T>::getPos(int& p, const T value) {for (int i = 0; i < curLen; i++)if (value == aList[i]) {p = i;return true;}return false;}template<class T>bool arrList<T>::append(const T value) {if (curLen >= maxSize) {cout<<"The list is overflow!"<<endl;return false;}aList[curLen++] = value;return true;}/* 1. if list not full, can insert * 2. if insertion legal, can insert * 3. from last to insertion, array elements move to next position * 4. insert */template<class T>bool arrList<T>::insert(const int p, const T value) {if (curLen >= maxSize) {cout<<"list is overflow!"<<endl;return false;}if (p < 0 || p > curLen) {cout<<"Insertion point is illegal!"<<endl;return false;}for (int i = curLen; i > p; i--) aList[i] = aList[i-1];aList[p] = value;curLen++;return true;}/* 1. if list is empty, cann't delete * 2. if deletion illegal, cann't delete * 3. delete:from deletion to last, array elements move forward one */template<class T>bool arrList<T>::deletee(const int p) {if (curLen <= 0) {cout<<"No element to delete!"<<endl;return false;}if (p < 0 || p > curLen-1) {cout<<"deletion is illegal!"<<endl;return false;}for (int i = p; i < curLen-1; i++) aList[i] = aList[i+1];curLen--;return true;}#endif

#include<iostream>#include"arrlist.h"using namespace std;intmain() {arrList<int> list(10);cout<<"The list is "<<(list.isEmpty()?"empty":"not empty")<<endl<<endl;cout<<"init list 0-4"<<endl;for (int i = 0; i < 5; i++) list.append(i);cout<<"The list is "<<(list.isEmpty()?"empty":"not empty")<<endl;cout<<"print the list:"<<endl;list.print();cout<<endl;cout<<"insert(0,5) (3, 5) (7, 5) (9, 5) (-3, 0)"<<endl;list.insert(0, 5);list.insert(3, 5);list.insert(7, 5);list.insert(9, 5);list.insert(-3, 0);list.print();cout<<endl;cout<<"setvalue(4, 5) (-4, 5) (33, 5)"<<endl;list.setValue(4, 5);list.setValue(-4, 5);list.setValue(33, 5);list.print();cout<<endl;cout<<"getvalue 0-curLen"<<endl;int v;for (int i = 0; i < list.length(); i++) {list.getValue(i, v);cout<<v<<" ";}cout<<endl;cout<<"getpos 5,4"<<endl;int p;list.getPos(p, 5);cout<<p<<" ";list.getPos(p, 4);cout<<p<<endl<<endl;cout<<"delete 0, -3, 9"<<endl;list.deletee(0);list.print();list.deletee(-3);list.deletee(9);cout<<endl;list.clear();list.isEmpty();return 0;}

0 0