模板——类模板

//《C++编程——数据结构与程序设计方法》15.8.3

#include<iostream>
#include <string>
#include "arrayListType.h"

using namespace std;


template<class elemType>
arrayListType<elemType>::arrayListType(int size)//(int size=100)是错的??
{
 if(size<0)
 {
  cout<<"Thearray size must be positive Creation "<<"an array of size 100. "<<endl;
  maxSize=100;
 }
 else
  maxSize=size;
 length=0;
 list=new elemType[maxSize];
}

template<class elemType>
arrayListType<elemType>::~arrayListType()
{
 delete [] list;
}

template<class elemType>
void arrayListType<elemType>::print() const
{
 int i;
 for(i=0;i<length;i++)
  cout<<list[i]<<" ";
 cout<<endl;
}

template<class elemType>
void arrayListType<elemType>::insertAt(int location,const elemType& insertItem)
{
 int i;
 if(location<0||location>=maxSize)
  cout<<"The position of the item to be inserted "<<"is out of range"<<endl;
 else
  if(length>=maxSize)
   cout<<"Cannot insert in a full list"<<endl;
  else
  {
   for(i=length;i>location;i--)
    list[i]=list[i-1];
   list[location]=insertItem;
   length++;
  }
}
template<class elemType>
void arrayListType<elemType>::removeAt(int location)
{
 int i;
 if(location<0||location>=maxSize)
  cout<<"The position of the item to be removeed "<<"is out of range"<<endl;
 else
 {
  for(i=location;i<length-1;i++)
   list[i]=list[i+1];
  length--;
 }
}

template<class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
 int loc;
 bool found=false;
 for(loc=0;loc<length;loc++)
  if(list[loc]==item)
  {
   found=true;
   break;
  }
 if(found)
  return loc;
 else
  return -1;
}

template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
 int loc;
 if(length==0)
  cout<<"Cannnot delete from an empty list."<<endl;
 else
 {
  loc=seqSearch(removeItem);
  if(loc!=-1)
   removeAt(loc);
  else
   cout<<"The item to be deleted is not in the lise."<<endl;
 }
}

template<class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator =(const arrayListType<elemType>& otherList)
{
 if(this!=&otherList)
 {
  if(maxSize!=otherList.maxSize)
   cout<<"Cannot copy The two arrays are of "<<"different sizes"<<endl;
  else
  {
   int j;
   maxSize=otherList.maxSize;
   length=otherList.length;
   list=new elemType[maxSize];
   if(length!=0)
    for(j=0;j<length;j++)
     list[j]=otherList.list[j];
  }
  return *this;
 }
}

//头文件arrayListType.h,据说类模板的实现部分只能做到.h   文件中

#ifndef _ARRAYLISTTYPE_H
#define _ARRAYLISTTYPE_H


template<class elemType>
class arrayListType
{
 public:
  const arrayListType<elemType>& operator=(const arrayListType<elemType>&);
//  bool isEmpty();
//  bool isFull();
//  int ListSize();
//  int ListSize();
//  int maxListSize();
  void print() const;
//  bool isItemAtEqual(int location,const elemType& item);
  void insertAt(int location,const elemType& insertItem);
//  void insertEnd(const elemType& insertItem);
  void removeAt(int location);
//  void retrieveAt(int location,elemType& retItem);
//  void replaceAt(int location,const elemType& repItem):
//  void clearList();
  int seqSearch(const elemType& item);
//  void insert(const elemType& insertItem);
  void remove(const elemType& removeItem);
  arrayListType(int size=100);
//  arrayListType(const arrayListType<elemType>& otherList);
  ~arrayListType();
 protected:
  elemType *list;
  int length;
  int maxSize;
};

template<class elemType>
arrayListType<elemType>::arrayListType(int size)//(int size=100)是错的??
{
 if(size<0)
 {
  cout<<"Thearray size must be positive Creation "<<"an array of size 100. "<<endl;
  maxSize=100;
 }
 else
  maxSize=size;
 length=0;
 list=new elemType[maxSize];
}

template<class elemType>
arrayListType<elemType>::~arrayListType()
{
 delete [] list;
}

template<class elemType>
void arrayListType<elemType>::print() const
{
 int i;
 for(i=0;i<length;i++)
  cout<<list[i]<<" ";
 cout<<endl;
}

template<class elemType>
void arrayListType<elemType>::insertAt(int location,const elemType& insertItem)
{
 int i;
 if(location<0||location>=maxSize)
  cout<<"The position of the item to be inserted "<<"is out of range"<<endl;
 else
  if(length>=maxSize)
   cout<<"Cannot insert in a full list"<<endl;
  else
  {
   for(i=length;i>location;i--)
    list[i]=list[i-1];
   list[location]=insertItem;
   length++;
  }
}
template<class elemType>
void arrayListType<elemType>::removeAt(int location)
{
 int i;
 if(location<0||location>=maxSize)
  cout<<"The position of the item to be removeed "<<"is out of range"<<endl;
 else
 {
  for(i=location;i<length-1;i++)
   list[i]=list[i+1];
  length--;
 }
}

template<class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
 int loc;
 bool found=false;
 for(loc=0;loc<length;loc++)
  if(list[loc]==item)
  {
   found=true;
   break;
  }
 if(found)
  return loc;
 else
  return -1;
}

template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
 int loc;
 if(length==0)
  cout<<"Cannnot delete from an empty list."<<endl;
 else
 {
  loc=seqSearch(removeItem);
  if(loc!=-1)
   removeAt(loc);
  else
   cout<<"The item to be deleted is not in the lise."<<endl;
 }
}

template<class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator =(const arrayListType<elemType>& otherList)
{
 if(this!=&otherList)
 {
  if(maxSize!=otherList.maxSize)
   cout<<"Cannot copy The two arrays are of "<<"different sizes"<<endl;
  else
  {
   int j;
   maxSize=otherList.maxSize;
   length=otherList.length;
   list=new elemType[maxSize];
   if(length!=0)
    for(j=0;j<length;j++)
     list[j]=otherList.list[j];
  }
  return *this;
 }
}


#endif

原文地址:https://www.cnblogs.com/WestGarden/p/3138420.html