• 模板——类模板


    //《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

  • 相关阅读:
    【转载】 HTTP 中 GET 与 POST 的区别
    JS 浏览器cookie的设置,读取,删除
    JS Event事件流(冒泡机制、捕获机制、事件绑定)
    DOM节点树和元素树--深度遍历
    Html_Task4(知识点:水平居中+垂直居中/position/float/border-radius)
    百度前端技术学院—斌斌学院题库
    百度前端技术学院—-小薇学院(HTML+CSS课程任务)
    js正则表达式
    js设计模式(一)发布订阅模式
    vue学习笔记(一)——利用vue-cli搭建一个前端项目
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138420.html
Copyright © 2020-2023  润新知