arrlist
2015-07-22 21:59
232 查看
/* 1. 插入删除等位置参数指数组下标 * 2. 判断条件有:表是否已满; 位置是否合法 */ #ifndef LIST_H #define LIST_H #include <iostream> using namespace std; template <class T> class arrList { private: T* aList; int maxSize; int curLen; int position; 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 subscript template<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; int main() { 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; }
相关文章推荐
- 算法学习之java实现归并排序
- sql笔记
- MFC中CWnd类及其派生类对话框、消息处理、窗口操作
- FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT用法
- Python GUI编程(Tkinter)
- iOS之沙盒机制和如何获取沙盒路径
- 对于一系列的高速算法设计(不允许使用数组)
- 快速排序 归并排序
- java的(PO,VO,TO,BO,DAO,POJO)解释
- hdu 3511 圆扫描线
- 【读书笔记】GCD-block
- Java集合框架(List, Set, Map, Collections)
- 学习一门语言难在什么地方?
- 多个相同参数表单提交
- Valid Parentheses
- [leedcode 118] Pascal's Triangle
- Openssl ec命令
- Objective-C学习笔记(五)——数据类型与限定词
- husterxsp.github.io
- 如何让你的APK跑在 com.android.phone 进程