国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++語言實現線性表之數組實例

2020-01-26 15:07:43
字體:
來源:轉載
供稿:網友

本文實例講述了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++程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪湖市| 宜兴市| 永寿县| 合川市| 凤山县| 古浪县| 双牌县| 乌兰察布市| 尖扎县| 临朐县| 依安县| 洞口县| 台东县| 常州市| 松阳县| 班玛县| 隆德县| 金华市| 嘉荫县| 都江堰市| 望江县| 轮台县| 鄱阳县| 白山市| 江油市| 班戈县| 历史| 武胜县| 南开区| 客服| 资阳市| 林甸县| 弥渡县| 巴彦淖尔市| 正镶白旗| 铜川市| 昌江| 安福县| 五寨县| 宁德市| 昆山市|