Advertisement
Falmung

listArray templates

Sep 15th, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. template<class T>
  2. int ListArray<T>::getLength() const{
  3.               return size;
  4. }//end getLength
  5.  
  6. template<class T>
  7. int ListArray<T>:: translate(int index) const
  8. {  
  9.      return (index - 1);
  10. }//end translate
  11.  
  12. template<class T>
  13. void ListArray<T>::insert(int index, T newItem, bool &success){
  14.      success = bool ((index >=1)&&(index<=size+1) && (size<MAXLIST));
  15.      if(success){
  16.          for(int pos=size;pos >=index;--pos)
  17.    items[translate(pos+1)]=items[translate(pos)];
  18.  
  19.          items[translate(index)]=newItem;
  20.          ++size;
  21.      }//end if
  22. } //end insert
  23.  
  24.  
  25. template<class T>
  26. void ListArray<T>::remove(int index, bool & success){
  27.     success = bool((index>=1) && (index<=size));
  28.     if(success){
  29.   for(int fromPosition = index+1; fromPosition<=size;++fromPosition)
  30.         items[translate(fromPosition-1)]=items[translate(fromPosition)];
  31.   --size;
  32.       }//end if
  33. } // end remove
  34.  
  35. template<class T>
  36. void ListArray<T>::retrieve(int index, T &dataItem, bool success) const{
  37.      success= bool((index >=1) && (index <= size));
  38.      if(success)
  39.    dataItem=items[translate(index)];
  40.       }//end retrieve
  41.  
  42.       template<class T>
  43.       void ListArray<T>::print() const{
  44.   for(int i=0;i<size;i++)
  45.         cout<<items[i]<<",";
  46.   cout << endl;
  47.   } //end print
  48.  
  49.  
  50.  
  51. // main.cpp
  52. #include<iostream>
  53. using namespace::std;
  54. #include "ListArray.h"
  55.  
  56. int main()
  57. {
  58.  ListArray<int> aList(6);
  59.  bool success;
  60.  int dato;
  61.  
  62.    aList.insert(1,10, success);
  63.    if(success)   aList.print();
  64.     else  cout<<"Out of Range\n";
  65.    aList.insert(2,23, success);
  66.    if(success)  aList.print();
  67.     else cout<<"Out of Range\n";
  68.    aList.insert(3,34,success);
  69.    if(success)  aList.print();
  70.    else cout<<"Out of Range\n";
  71.    aList.insert(3,67,success);
  72.    aList.print();
  73.    aList.insert(3,78,success);
  74.    aList.print();
  75.    aList.insert(3,34,success);
  76.    aList.print();
  77.    cout<<"Array size:"<<aList.getLength()<<endl;
  78.    aList.retrieve(2,dato,success);
  79.   cout<<"Dato:"<<dato<<endl;
  80.    aList.remove(3,success);
  81.    if(success) aList.print();
  82.    else cout<<"Out of Range\n";
  83.    cout<<"Array size:"<<aList.getLength()<<endl;  
  84.    return 0;
  85. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement