C++语言实现线性表之数组实例

所属分类: 软件编程 / C 语言 阅读数: 71
收藏 0 赞 0 分享

本文实例讲述了C++语言实现线性表之数组。分享给大家供大家参考。具体分析如下:

感觉用C++中的构造函数、析构函数等类的特点来描述一些数据结构更加易读,更加合理,便捷。但有一个问题,编译器不支持模板的分离编译,很不舒服

#include <iostream>
using namespace std;
template<class T>
class CArray
{
public:
 CArray(const int &iMax);
 CArray();
 ~CArray();
 void Create(const int &iMax);
 void Destroy();
 void Print();
 bool IsEmpty();
 bool IsFull();
 void Append(const T &data);
 int GetLength();
 int GetMax();
 bool Delete(const int &pos);
 bool Insert(const int &pos,const T &data);
 void operator+=(const T &data);
private:
 T *m_pArray;
 int m_len;
 int m_max;
 void Reset();
};
template<class T>
CArray<T>::CArray(const int &iMax)
{
 Create(iMax);
}
template<class T>
CArray<T>::~CArray()
{
 Destroy();
}
template<class T>
void CArray<T>::Create(const int &iMax)
{
 m_pArray = new T[iMax];
 m_max = iMax;
 m_len = 0;
 memset(m_pArray,0,sizeof(m_pArray));
}
template<class T>
void CArray<T>::Destroy()
{
 delete [] m_pArray;
}
template<class T>
void CArray<T>::Print()
{
 if(IsEmpty())
 { 
  cout<<"没有数据!"<<endl;
 }
 else
 {
  for(int ix =0 ; ix < m_len ; ++ix)
  {
   cout<<m_pArray[ix]<<",";
  }
  cout<<endl;
 }
}
template<class T>
bool CArray<T>::IsEmpty()
{
 if(0 == m_len)
 {
  return true;
 }
 else
 {
  return false;
 }
}
template<class T>
bool CArray<T>::IsFull()
{
 if(m_len == m_max)
 {
  Reset();
  return false;
 }
 else
 {
  return false;
 }
}
template<class T>
void CArray<T>::Append(const T &data)
{
 if(!IsFull())
 {
  ++m_len;
  m_pArray[m_len - 1] = data;
 }
}
template<class T>
int CArray<T>::GetLength()
{
 return m_len;
}
template<class T>
bool CArray<T>::Delete(const int &pos)
{
 if(pos > m_len || pos <= 0)
 {
  cout<<"位置不合法"<<endl;
  return false;
 }
 for(int ix = pos - 1 ; ix < m_len - 1 ; ++ ix)
 {
  m_pArray[ix] = m_pArray[ix + 1];
 }
 --m_len;
 return true;
}
template<class T>
void CArray<T>::operator+=(const T &data)
{
 this->Append(data);
}
template<class T>
bool CArray<T>::Insert(const int &pos,const T &data)
{
 if(IsFull())
 {
  return false;
 }
 else
 {
  for(int ix = m_len - 1 ; ix >= pos - 1 ; -- ix)
  {
   m_pArray[ix + 1] = m_pArray[ix];  
  }
  m_pArray[pos - 1] = data;
  ++m_len;
  return true;
 }
}
template<class T>
CArray<T>::CArray()
{
 Create(5);
}
template<class T>
void CArray<T>::Reset()
{
 T *pT = new T[m_max * 2];
 memset(pT,0,sizeof(pT));
 for(int ix = 0 ; ix < m_len ; ++ ix)
 {
  pT[ix] = m_pArray[ix];
 }
 delete [] m_pArray;
 
 m_pArray = pT;
 m_max = m_max * 2;
}
template<class T>
int CArray<T>::GetMax()
{
 return m_max;
}

希望本文所述对大家的C++程序设计有所帮助。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多