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

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

C++順序表的實例代碼

2020-05-23 13:32:05
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C++實現順序表的具體代碼,供大家參考,具體內容如下

#include <iostream> using namespace std;  typedef int DataType;  class SeqList { public:   SeqList()     :_a(NULL)     , _size(0)     , _capacity(0)   {}    SeqList(const SeqList& s)     :_a(new DataType[s._size])     , _size(s._size)     , _capacity(s._capacity)   {     memcpy(_a, s._a, sizeof(DataType)*s._size);   }    SeqList& operator=(const SeqList& s)   {     if (this != &s)     {       DataType* tmp = new DataType[s._size];       delete[] _a;       _a = tmp;       memcpy(_a, s._a, sizeof(DataType)*s._size);       _size = s._size;       _capacity = s._capacity;     }          return *this;   }    //SeqList& operator=(SeqList s)  //若傳引用會改變引用對象的值   //{   // swap(_a, s._a);   // swap(_size, s._size);   // swap(_capacity, s._capacity);    // return *this;   //}    ~SeqList()   {     if (_a)     {       delete[] _a;     }   }    void PushBack(DataType d)   {     CheckCapacity();     _a[_size] = d;     _size++;   }    void PopBack()   {     if (_size > 0)     {       _size--;     }     else     {       cout << "順序表為空" << endl;     }   }    void PushFront(DataType d)   {     CheckCapacity();      int i = (int)_size;     for (; i > 0; i--)     {       _a[i] = _a[i - 1];     }     _a[0] = d;     ++_size;   }    void PopFront()   {     if (_size > 0)     {       int i = 0;       for (; i < (int)_size; i++)       {         _a[i] = _a[i + 1];       }       _size--;     }     else     {       cout << "順序表為空" << endl;     }   }    void Print()   {     if (_size > 0)     {       int i = 0;       for (; i < (int)_size; i++)       {         cout << _a[i] << " ";       }       cout << endl;     }     else     {       cout << "順序表為空" << endl;     }   }    void Insert(size_t pos, DataType d) //在pos之前插入一個數據   {     CheckCapacity();      if (_size > 0)     {       if (pos <= 0 || pos > _size)        {         cout << "pos位置非法" << endl;       }       else       {         int i = 0;         for (i = (int)_size + 1; i > pos - 1; i--)         {           _a[i] = _a[i - 1];         }         _a[pos - 1] = d;         _size++;       }     }     else     {       PushFront(d);     }        }    void Erase(size_t pos) //刪除pos位置的數據   {     if (_size > 0)     {       if (pos <= 0 || pos > _size)       {         cout << "pos位置非法" << endl;       }       else       {         int i = pos - 1;         for (; i < (int)_size; i++)         {           _a[i] = _a[i + 1];         }         _size--;       }     }     else     {       cout << "順序表為空,無法進行刪除" << endl;     }   }    int Find(DataType d)   {     int i = 0;          for (; i < (int)_size; i++)     {       if (_a[i] == d)       {         return i + 1;       }     }     return 0;   }  private:   void CheckCapacity()   {     if (_size == _capacity)     {       _capacity = _capacity * 2 + 3;       _a = (DataType*)realloc(_a, sizeof(DataType)*_capacity);     }   }  private:   DataType* _a;   size_t _size;   size_t _capacity; }; 

以下為測試函數

#include "SeqList.h";  void Test1() {   SeqList s1;   s1.PushBack(1);   s1.PushBack(2);   s1.PushBack(3);   s1.PushBack(4);   s1.Print();   SeqList s2(s1);   s2.Print();   s2.PopBack();   s2.PopBack();   s2.PopBack();   s2.PopBack();   s2.PopBack();   s2.Print();   s2.PushFront(4);   s2.PushFront(3);   s2.PushFront(2);   s2.PushFront(1);   s2.Print();   s2.PopFront();   s2.Print();   s2.PopFront();   s2.PopFront();   s2.PopFront();   s2.PopFront();   s2.PopFront();   SeqList s3;   s3 = s1;   s3.Print(); }  void Test2() {   SeqList s1;   s1.PushBack(1);   s1.PushBack(2);   s1.PushBack(3);   s1.PushBack(4);   s1.Print();    //s1.Insert(1, 0);   //s1.Print();    /*s1.Erase(1);   s1.Erase(1);   s1.Erase(1);   s1.Erase(1);   s1.Print();*/    int i = s1.Find(5);   cout << i << endl; }  int main() {   //Test1();   Test2();    system("pause");   return 0; } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邮箱| 阳城县| 海淀区| 溆浦县| 崇信县| 临猗县| 河间市| 论坛| 翁源县| 和田县| 黔西| 内丘县| 轮台县| 交城县| 土默特右旗| 阿克| 子长县| 新闻| 陆良县| 葫芦岛市| 乌拉特前旗| 峨边| 顺昌县| 临夏市| 明光市| 阿勒泰市| 红桥区| 无为县| 布尔津县| 夏河县| 凤冈县| 获嘉县| 泰和县| 县级市| 元阳县| 天等县| 隆林| 宁城县| 杭州市| 象州县| 太保市|