C++語言實現線性表之數組實例
本文實例講述了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++程序設計有所幫助。
上一篇:vc提示unexpected end of file found的原因分析
欄 目:C語言
本文標題:C++語言實現線性表之數組實例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3089.html
您可能感興趣的文章
- 04-02c語言函數調用后清空內存 c語言調用函數刪除字符
- 04-02c語言的正則匹配函數 c語言正則表達式函數庫
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言中對數函數的表達式 c語言中對數怎么表達
- 04-02c語言用函數寫分段 用c語言表示分段函數
- 04-02c語言編寫函數冒泡排序 c語言冒泡排序法函數
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段函數
- 04-02C語言中怎么打出三角函數 c語言中怎么打出三角函數的值
- 04-02c語言調用函數求fibo C語言調用函數求階乘


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結
- 01-11ajax實現頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery


