List(2)List的顺序实现

来源:互联网 发布:大智慧贵金属分析软件 编辑:程序博客网 时间:2024/06/05 17:35

list.h


/*-----------------------------------------------   Created By EverSteins   Email:EverSteins@gmail.com  转载请注明出处  ------------------------------------------------*/ template<class List_entry>class List{public:List():count(0);int size() const;bool full() const;bool empty() const;void clear();void traverse(void (*visit)(List_entry&));Error_code retrieve(int position,List_entry &x)const;Error_code replace(int position,const List_entry &x);Error_code remove(int position,List_entry &x);Error_code insert(int position,const List_entry &x);protected:int count;List_entry entry[max_list];};


list.cc

/*-----------------------------------------------   Created By EverSteins   Email:EverSteins@gmail.com  转载请注明出处  ------------------------------------------------*/ #include "list.h"template<class List_entry>int List<List_entry>::size() const{return count;}template<class List_entry>bool List<List_entry>::full() const{return count>=max_list;}template<class List_entry>bool List<List_entry>::empty() const{return count<=0;}template<class List_entry>void List<List_entry>::clear(){count=0;}template<class List_entry>void List<List_entry>::traverse(void (*visit)(List_entry&)){for (int i=0;i<count;i++)(*visit)(entry[i]);}template<class List_entry>Error_code List<List_entry>::insert(int position,const List_entry &x){if (full())return overflow;if (position < 0 || position > count)return range_error;for (int i=count-1;i>=position;i--)entry[i+1]=entry[i];entry[position]=x;count++;return success;}template<class List_entry>Error_code List<List_entry>::remove(int position,List_entry &x){if (empty())return underflow;if (position<0 || position>=count)return range_error;x=entry[position];for (int i=position;i<count-1;i++)entry[i]=entry[i+1];count--;return success;}template<class List_entry>Error_code List<List_entry>::retrieve(int position,List_entry &x)const{if (position<0 || position>=count)return range_error;x=entry[position];return success;}template<class List_entry>Error_code List<List_entry>::replace(int position,const List_entry &x){if (position<0 || position>=count)return range_error;entry[position]=x;return success;}






原创粉丝点击