利用C++簡單實現(xiàn)順序表和單鏈表的示例代碼
更新時間:2017年08月01日 11:32:23 作者:Suhw
這篇文章主要給大家介紹了關(guān)于利用C++簡單實現(xiàn)順序表和單鏈表的方法,文中給出了詳細的示例代碼供大家參考學習,需要的朋友可以參考借鑒,下面來跟著小編一起來學習學習吧。
本文主要給大家介紹了關(guān)于C++實現(xiàn)順序表和單鏈表的相關(guān)內(nèi)容,分享出來供大家參考學習,話不多說,來一起看看詳細的介紹:
一、順序表示例代碼:
#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); } //[]運算符重載 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é)點)示例代碼
#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é)點在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)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持
相關(guān)文章
《C++ Primer》隱式類類型轉(zhuǎn)換學習整理
在本篇文章里小編給大家整理的是關(guān)于《C++ Primer》隱式類類型轉(zhuǎn)換學習筆記內(nèi)容,需要的朋友們參考下。2020-02-02Sersync+Rsync實現(xiàn)觸發(fā)式文件同步實戰(zhàn)過程
sersync是使用c++編寫,而且對linux系統(tǒng)文 件系統(tǒng)產(chǎn)生的臨時文件和重復的文件操作進行過濾。下面通過本文給大家分享Sersync+Rsync實現(xiàn)觸發(fā)式文件同步實戰(zhàn)過程,需要的朋友參考下吧2017-09-09C語言數(shù)據(jù)結(jié)構(gòu)系列隊列篇
本章我們將學習 "隊列" ,首先介紹隊列的概念和結(jié)構(gòu),然后我們將著重講解棧的實現(xiàn)。我們從零開始寫隊列的接口,并從零開始步步解讀。本章將繼續(xù)鞏固畫思路草圖的能力,只要思路草圖畫好了,就可以很輕松地將其轉(zhuǎn)換成代碼2022-02-02仿現(xiàn)代C++智能指針實現(xiàn)引用計數(shù)
這篇文章主要為大家詳細介紹了如何仿現(xiàn)代C++智能指針實現(xiàn)引用計數(shù),文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以了解下2024-03-03