Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LIST_H
- #define LIST_H
- typedef int EType ;
- typedef int Position;
- class List {
- public:
- List();
- void insert(EType x, Position p);
- void remove(Position p);
- bool isEmpty() const;
- Position locate(EType x) const;
- EType retrieve(Position p) const;
- Position first() const;
- Position end() const;
- Position next(Position p) const;
- Position previous(Position p) const;
- void clear();
- void print() const;
- private:
- static const int MAX_SIZE = 100;
- int array[MAX_SIZE];
- int length;
- };
- #endif
- #include <iostream>
- #include <cstdlib>
- #include "list.h"
- using namespace std;
- void List::insert(EType x, Position p){
- if(p<first() || p>end())
- cout<<"Error insert"<<endl;
- for(Position i=previous(end());i>=p;i=previous(i))
- array[next(i)]=retrieve(i);
- array[p]=x;
- length;
- }
- void List::remove(Position p){
- }
- bool List::isEmpty() const{
- bool empty=false;
- for(int i=0;i<MAX_SIZE;i++){
- if(length==0)
- empty=true;
- }
- return empty;
- }
- Position List:: locate(EType x) const{
- Position p=first();
- while(p!=end()){
- if(retrieve(p)==x)
- return p;
- }
- }
- EType List:: retrieve(Position p) const{
- Position x=first();
- while(x!=end()){
- if(retrieve(x)==p)
- return x;
- }
- }
- Position List::first() const{
- return 0;
- }
- Position List::end() const{
- return length;
- }
- Position List::next(Position p) const{
- if(p>=end())
- cout<<"Error"<<endl;
- return p+1;
- }
- Position List::previous(Position p) const{
- if(p<=first())
- cout<<"Error"<<endl;
- return p-1;
- }
- void List::clear(){
- for(int i=0;i<MAX_SIZE;i++){
- array[i]=0;
- }
- }
- void List::print() const{
- bool empty=false;
- for(int i=0;i<MAX_SIZE;i++){
- if(array[i]!=first()){
- cout<<"Position: "<<i+1<<". "<<array[i]<<endl;
- }
- }
- if(empty==true){
- cout<<"The list is empty"<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement