当前位置:首页 > 通信资讯 > 正文

构建线性表的c语言代码(C语言实现线性表)

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

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 #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++程序设计有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。