Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #ifndef LIST_H
  2. #define LIST_H
  3.  
  4. typedef int EType ;
  5. typedef int Position;
  6.  
  7. class List {
  8. public:
  9.     List();
  10.     void insert(EType x, Position p);
  11.     void remove(Position p);
  12.    
  13.     bool isEmpty() const;
  14.    
  15.     Position locate(EType x) const;
  16.     EType retrieve(Position p) const;
  17.    
  18.     Position first() const;
  19.     Position end() const;
  20.     Position next(Position p) const;
  21.     Position previous(Position p) const;
  22.    
  23.     void clear();
  24.    
  25.     void print() const;
  26. private:
  27.     static const int MAX_SIZE = 100;
  28.     int array[MAX_SIZE];
  29.     int length;
  30. };
  31.  
  32. #endif
  33. #include <iostream>
  34. #include <cstdlib>
  35. #include "list.h"
  36.  
  37. using namespace std;
  38.  
  39. void List::insert(EType x, Position p){
  40.     if(p<first() || p>end())
  41.         cout<<"Error insert"<<endl;
  42.     for(Position i=previous(end());i>=p;i=previous(i))
  43.         array[next(i)]=retrieve(i);
  44.             array[p]=x;
  45.             length;
  46.             }
  47. void List::remove(Position p){
  48.    
  49.     }
  50.    
  51.  
  52.  
  53. bool List::isEmpty() const{
  54.     bool empty=false;
  55.     for(int i=0;i<MAX_SIZE;i++){
  56.         if(length==0)
  57.             empty=true;
  58. }
  59. return empty;
  60. }
  61.  
  62. Position List:: locate(EType x) const{
  63.     Position p=first();
  64.     while(p!=end()){
  65.         if(retrieve(p)==x)
  66.             return p;
  67.     }
  68. }
  69. EType List:: retrieve(Position p) const{
  70.     Position x=first();
  71.     while(x!=end()){
  72.         if(retrieve(x)==p)
  73.             return x;
  74.     }
  75. }
  76.    
  77. Position List::first() const{
  78.     return 0;
  79. }
  80. Position List::end() const{
  81.     return length;
  82. }
  83. Position List::next(Position p) const{
  84.     if(p>=end())
  85.         cout<<"Error"<<endl;
  86.     return p+1;
  87. }
  88. Position List::previous(Position p) const{
  89.     if(p<=first())
  90.         cout<<"Error"<<endl;
  91.     return p-1;
  92. }
  93. void List::clear(){
  94.     for(int i=0;i<MAX_SIZE;i++){
  95.         array[i]=0;
  96.     }
  97. }
  98. void List::print() const{
  99.     bool empty=false;
  100.     for(int i=0;i<MAX_SIZE;i++){
  101.         if(array[i]!=first()){
  102.             cout<<"Position: "<<i+1<<". "<<array[i]<<endl;
  103.         }
  104. }
  105. if(empty==true){
  106.     cout<<"The list is empty"<<endl;
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement