利用C++簡單實現(xiàn)順序表和單鏈表的示例代碼
本文主要給大家介紹了關(guān)于C++實現(xiàn)順序表和單鏈表的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,來一起看看詳細(xì)的介紹:
一、順序表示例代碼:
#include <assert.h> #include <iostream> using namespace std; typedef int Datatype; class SeqList { public: SeqList() :_array(NULL) ,_size(0) ,_capacity(0) { } SeqList(const SeqList& s) { _array = (Datatype*)malloc(s._size*(sizeof(Datatype))); memcpy(_array, s._array, s._size*(sizeof(Datatype))); _size = _capacity = s._size; } SeqList& operator=(SeqList& s) { free(_array); Swap(s); return *this; } void Swap(SeqList& s) { _array = s._array; _size = s._size; _capacity = s._capacity; } ~SeqList() { if (_array) { free(_array); _array = NULL; _size = _capacity = 0; } } void Print() { for (size_t i = 0; i < _size; i++) { cout << _array[i] << " "; } cout << endl; } void CheckCapcacity() { if (_size == _capacity) { _capacity = 2 * _capacity + 3; _array = (Datatype*)realloc(_array, _capacity*sizeof(Datatype)); assert(_array); } } //后插 void PushBack(Datatype x) { Insert(_size, x); } //前插 void PushFront(Datatype x) { Insert(0, x); } //刪除最后一個 void PopBack() { Erase(_size); } //刪除第一個 void PopFront() { Erase(0); } //[]運(yùn)算符重載 Datatype& operator[](size_t pos) { assert(pos < _size); return _array[pos]; } //pos位置前插入x void Insert(size_t pos, Datatype x) { assert(pos <= _size); CheckCapcacity(); int end = (int)_size - 1; if (pos == 0) { while (end >= 0) { _array[end + 1] = _array[end]; end--; } _array[0] = x; } else { while (end >= (int)pos) { _array[end + 1] = _array[end]; end--; } _array[pos] = x; } _size++; } //刪除pos位置的元素 void Erase(size_t pos) { assert(pos < _size); //popfront的實現(xiàn) if (_size > 0) { if (pos == 0) { int end = 0; while (end < (int)_size - 1) { _array[end] = _array[end + 1]; end++; } _size--; } //popback的實現(xiàn) else if (pos == _size) { _size--; } //erase else { int end = pos; while (end < (int)_size - 1) { _array[end] = _array[end + 1]; end++; } _size--; } } return; } private: Datatype* _array; size_t _size; size_t _capacity; };
二、單鏈表(不含頭結(jié)點(diǎn))示例代碼
#include <iostream> #include <assert.h> using namespace std; typedef int DataType; struct SListNode { SListNode* _next; DataType _data; SListNode(DataType x) :_data(x) , _next(NULL) {} }; typedef SListNode Node; class SList { public: SList() :_head(NULL) , _tail(NULL) {} SList(const SList& s) :_head(NULL) ,_tail(NULL) { Copy(s); } SList& operator=(const SList& s) { Destroy(); Copy(s); return *this; } ~SList() { Destroy(); } void Copy(const SList& s) { Node* cur = s._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } void Destroy() { Node* cur = _head; while (_head != NULL) { cur = _head; _head = cur->_next; delete cur; } _head = _tail = NULL; } void PushBack(DataType x) { if ((_head == NULL)&&(_tail == NULL)) { _head = _tail = new Node(x); } else { _tail->_next = new Node(x); _tail = _tail->_next; } } void PopBack() { if (_head == NULL) { return; } else if (_head ->_next == NULL) { delete _head; _head = _tail = NULL; } else { Node* tmp = _head; while (tmp->_next->_next != NULL) { tmp = tmp->_next; } _tail = tmp; tmp->_next = NULL; } } void PushFront(DataType x) { if ((_head == NULL) && (_tail == NULL)) { _head = _tail = new Node(x); } else { Node* tmp = new Node(x); tmp->_next = _head; _head = tmp; } } void PopFront() { if (_head == NULL) { return; } Node* cur = _head; _head = _head->_next; delete cur; } Node* Find(DataType x) { Node* tmp = _head; while (tmp) { if (tmp->_data == x) return tmp; tmp = tmp->_next; } return NULL; } // 插入一個節(jié)點(diǎn)在pos的前面 void Insert(Node* pos, DataType x) { assert(pos); if (pos == 0) { PushFront(x); } else { Node* cur = _head; while (cur->_next != pos) { cur = cur->_next; } Node* tmp = new Node(x); tmp->_next = pos; cur->_next = tmp; } } void Erase(Node* pos) { assert(pos); if (pos == 0) { PopFront(); } else if (pos->_next == NULL) { PopBack(); } else { Node* cur = _head; while (cur->_next != pos) { cur = cur->_next; } Node* tmp = cur->_next; cur->_next = tmp->_next; delete tmp; } } void Print() { Node* tmp = _head; while (tmp != NULL) { cout <<tmp->_data << "->"; tmp= tmp->_next; } cout <<"NULL"<<endl; } private: Node* _head; Node* _tail; };
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對我們的支持
欄 目:C語言
下一篇:C++面試題之結(jié)構(gòu)體內(nèi)存對齊計算問題總結(jié)大全
本文標(biāo)題:利用C++簡單實現(xiàn)順序表和單鏈表的示例代碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1282.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法