欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++語言實(shí)現(xiàn)線性表之?dāng)?shù)組實(shí)例

 更新時間:2015年04月20日 11:14:50   作者:司青  
這篇文章主要介紹了C++語言實(shí)現(xiàn)線性表之?dāng)?shù)組,實(shí)例分析了C++實(shí)現(xiàn)數(shù)組形式線性表的原理與方法,需要的朋友可以參考下

本文實(shí)例講述了C++語言實(shí)現(xiàn)線性表之?dāng)?shù)組。分享給大家供大家參考。具體分析如下:

感覺用C++中的構(gòu)造函數(shù)、析構(gòu)函數(shù)等類的特點(diǎn)來描述一些數(shù)據(jù)結(jié)構(gòu)更加易讀,更加合理,便捷。但有一個問題,編譯器不支持模板的分離編譯,很不舒服

#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<<"沒有數(shù)據(jù)!"<<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++程序設(shè)計有所幫助。

相關(guān)文章

  • C++模擬2D牛頓力學(xué)效果的示例代碼

    C++模擬2D牛頓力學(xué)效果的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C++模擬2D牛頓力學(xué)效果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2023-06-06
  • vc提示unexpected end of file found的原因分析

    vc提示unexpected end of file found的原因分析

    這篇文章主要介紹了vc提示unexpected end of file found的原因分析,給出了幾點(diǎn)常見錯誤原因的分析,需要的朋友可以參考下
    2015-05-05
  • 手把手教你如何一眼分辨是C還是C++

    手把手教你如何一眼分辨是C還是C++

    在很大程度上,C++是C的超集,這意味著一個有效的C程序也是一個有效的C++程序,下面這篇文章主要給大家介紹了關(guān)于如何一眼分辨是C還是C++的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • C++實(shí)現(xiàn)LeetCode(557.翻轉(zhuǎn)字符串中的單詞之三)

    C++實(shí)現(xiàn)LeetCode(557.翻轉(zhuǎn)字符串中的單詞之三)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(557.翻轉(zhuǎn)字符串中的單詞之三),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C語言實(shí)踐設(shè)計開發(fā)飛機(jī)游戲

    C語言實(shí)踐設(shè)計開發(fā)飛機(jī)游戲

    飛機(jī)大戰(zhàn)想必是很多人童年時期的經(jīng)典游戲,我們依舊能記得抱個老人機(jī)娛樂的場景,下面這篇文章主要給大家介紹了關(guān)于如何利用C語言寫一個簡單的飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • C++一個函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)

    C++一個函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)

    這篇文章主要介紹了C++一個函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C語言實(shí)現(xiàn)倉庫物資管理系統(tǒng)

    C語言實(shí)現(xiàn)倉庫物資管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)倉庫物資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C++ Boost MPI接口詳細(xì)講解

    C++ Boost MPI接口詳細(xì)講解

    Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱
    2022-11-11
  • 擴(kuò)展KMP算法(Extend KMP)

    擴(kuò)展KMP算法(Extend KMP)

    我們這里說的KMP不是拿來放電影的(雖然我很喜歡這個軟件),而是一種算法。KMP算法是拿來處理字符串匹配的。今天我們談到的是對KMP算法的拓展
    2014-08-08
  • C++基于EasyX框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲

    C++基于EasyX框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲

    EasyX是針對C/C++的圖形庫,可以幫助使用C/C++語言的程序員快速上手圖形和游戲編程。本文將利用EasyX框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲,需要的可以參考一下
    2023-01-01

最新評論