Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int maxlist=1000;
- enum Error_code
- {
- success,
- overflow,
- underflow,
- range_error
- };
- template <class List_entry>
- class List
- {
- public:
- //List();
- List():count(0)
- {}
- bool empty()
- {
- return count==0;
- }
- bool full()
- {
- return count==maxlist;
- }
- int size()
- {
- return count;
- }
- Error_code 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;
- }
- Error_code retrieve(int position, List_entry &x)const
- {
- if(empty()) return underflow;
- if(position<0 || position>count) return range_error;
- x=entry[position];
- return success;
- }
- Error_code replace(int position, const List_entry &x)
- {
- if(empty()) return underflow;
- if(position<0 || position>count) return range_error;
- entry[position]=x;
- return success;
- }
- Error_code remove(int position, List_entry &x)
- {
- if(empty()) return underflow;
- if(position>0 || position>=count) return range_error;
- for(int i=position;i<=count-1;i++)
- entry[i+1]=entry[i];
- count--;
- return success;
- }
- void traverse(void (*visit)(List_entry &x))//*visit is the pointer to function which performs operation on list
- {
- for(int i=0;i<count;i++)
- (*visit)(entry[i]);
- }
- protected:
- int count;
- List_entry entry[maxlist];
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement