亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++順序表的實(shí)例代碼

 更新時(shí)間:2020年05月22日 16:00:51   作者:tttjp  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)順序表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)順序表的具體代碼,供大家參考,具體內(nèi)容如下

#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) //若傳引用會(huì)改變引用對(duì)象的值 
 //{ 
 // 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之前插入一個(gè)數(shù)據(jù) 
 { 
 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位置的數(shù)據(jù) 
 { 
 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 << "順序表為空,無(wú)法進(jìn)行刪除" << 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; 
}; 

以下為測(cè)試函數(shù)

#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; 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語(yǔ)言進(jìn)階可變參數(shù)列表

    C語(yǔ)言進(jìn)階可變參數(shù)列表

    這篇文章主要為大家介紹了C語(yǔ)言進(jìn)階可變參數(shù)列表的示例詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • 關(guān)于Visual Studio無(wú)法打開(kāi)源文件

    關(guān)于Visual Studio無(wú)法打開(kāi)源文件"stdio.h"問(wèn)題

    這篇文章主要介紹了關(guān)于Visual Studio無(wú)法打開(kāi)源文件"stdio.h"問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Qt實(shí)現(xiàn)http服務(wù)的示例代碼

    Qt實(shí)現(xiàn)http服務(wù)的示例代碼

    這篇文章將為大家詳細(xì)講解有關(guān)Qt如何實(shí)現(xiàn)http服務(wù),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲
    2023-04-04
  • C++11 學(xué)習(xí)筆記之std::function和bind綁定器

    C++11 學(xué)習(xí)筆記之std::function和bind綁定器

    這篇文章主要介紹了C++11 學(xué)習(xí)筆記之std::function和bind綁定器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • C和C++中argc和argv的含義及用法詳解

    C和C++中argc和argv的含義及用法詳解

    argv 是 argument vector的縮寫,表示傳入main函數(shù)的參數(shù)序列或指針,這篇文章主要介紹了C和C++中argc和argv的含義以及用法,需要的朋友可以參考下
    2022-11-11
  • C語(yǔ)言實(shí)現(xiàn)小貓釣魚(yú)算法

    C語(yǔ)言實(shí)現(xiàn)小貓釣魚(yú)算法

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)小貓釣魚(yú)算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 實(shí)例分析一個(gè)簡(jiǎn)單的Win32程序

    實(shí)例分析一個(gè)簡(jiǎn)單的Win32程序

    這篇文章主要介紹了實(shí)例分析一個(gè)簡(jiǎn)單的Win32程序,對(duì)于Win32應(yīng)用程序的原理、執(zhí)行流程、實(shí)現(xiàn)方法主要環(huán)節(jié)都做了較為詳細(xì)的分析,有助于讀者深入理解Windows應(yīng)用程序設(shè)計(jì),需要的朋友可以參考下
    2014-09-09
  • C語(yǔ)言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式詳解

    C語(yǔ)言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式詳解

    任何數(shù)據(jù)在內(nèi)存中都是以二進(jìn)制的形式存儲(chǔ)的,例如一個(gè)short型數(shù)據(jù)1156,其二進(jìn)制表示形式為00000100 10000100,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式,需要的朋友可以參考下
    2023-03-03
  • 數(shù)據(jù)結(jié)構(gòu)之Treap詳解

    數(shù)據(jù)結(jié)構(gòu)之Treap詳解

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之Treap詳解,本文講解了Treap的基本知識(shí)、Treap的基本操作、Treap的高級(jí)操作技巧等,需要的朋友可以參考下
    2014-08-08
  • C/C++ 原生API實(shí)現(xiàn)線程池的方法

    C/C++ 原生API實(shí)現(xiàn)線程池的方法

    線程池,簡(jiǎn)單來(lái)說(shuō)就是有一堆已經(jīng)創(chuàng)建好的線程,接下來(lái)通過(guò)本文給大家介紹C/C++ 原生API實(shí)現(xiàn)線程池的方法,感興趣的朋友跟隨小編一起看看吧
    2021-11-11

最新評(píng)論