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 << "順序表為空,無法進(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;
};
以下為測試函數(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;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Visual Studio無法打開源文件"stdio.h"問題
這篇文章主要介紹了關(guān)于Visual Studio無法打開源文件"stdio.h"問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Qt實(shí)現(xiàn)http服務(wù)的示例代碼
這篇文章將為大家詳細(xì)講解有關(guān)Qt如何實(shí)現(xiàn)http服務(wù),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲2023-04-04
C++11 學(xué)習(xí)筆記之std::function和bind綁定器
這篇文章主要介紹了C++11 學(xué)習(xí)筆記之std::function和bind綁定器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
C語言浮點(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語言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式,需要的朋友可以參考下2023-03-03
數(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)線程池的方法
線程池,簡單來說就是有一堆已經(jīng)創(chuàng)建好的線程,接下來通過本文給大家介紹C/C++ 原生API實(shí)現(xiàn)線程池的方法,感興趣的朋友跟隨小編一起看看吧2021-11-11

