Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- int ListArray<T>::getLength() const{
- return size;
- }//end getLength
- template<class T>
- int ListArray<T>:: translate(int index) const
- {
- return (index - 1);
- }//end translate
- template<class T>
- void ListArray<T>::insert(int index, T newItem, bool &success){
- success = bool ((index >=1)&&(index<=size+1) && (size<MAXLIST));
- if(success){
- for(int pos=size;pos >=index;--pos)
- items[translate(pos+1)]=items[translate(pos)];
- items[translate(index)]=newItem;
- ++size;
- }//end if
- } //end insert
- template<class T>
- void ListArray<T>::remove(int index, bool & success){
- success = bool((index>=1) && (index<=size));
- if(success){
- for(int fromPosition = index+1; fromPosition<=size;++fromPosition)
- items[translate(fromPosition-1)]=items[translate(fromPosition)];
- --size;
- }//end if
- } // end remove
- template<class T>
- void ListArray<T>::retrieve(int index, T &dataItem, bool success) const{
- success= bool((index >=1) && (index <= size));
- if(success)
- dataItem=items[translate(index)];
- }//end retrieve
- template<class T>
- void ListArray<T>::print() const{
- for(int i=0;i<size;i++)
- cout<<items[i]<<",";
- cout << endl;
- } //end print
- // main.cpp
- #include<iostream>
- using namespace::std;
- #include "ListArray.h"
- int main()
- {
- ListArray<int> aList(6);
- bool success;
- int dato;
- aList.insert(1,10, success);
- if(success) aList.print();
- else cout<<"Out of Range\n";
- aList.insert(2,23, success);
- if(success) aList.print();
- else cout<<"Out of Range\n";
- aList.insert(3,34,success);
- if(success) aList.print();
- else cout<<"Out of Range\n";
- aList.insert(3,67,success);
- aList.print();
- aList.insert(3,78,success);
- aList.print();
- aList.insert(3,34,success);
- aList.print();
- cout<<"Array size:"<<aList.getLength()<<endl;
- aList.retrieve(2,dato,success);
- cout<<"Dato:"<<dato<<endl;
- aList.remove(3,success);
- if(success) aList.print();
- else cout<<"Out of Range\n";
- cout<<"Array size:"<<aList.getLength()<<endl;
- return 0;
- }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement