文章目錄 1. list的構造 2. list迭代器的常見接口 2.1 list遍歷的迭代器接口 2.2 list修改數據的迭代器接口 2.3 list排序、逆序、合并相關操作的成員函數 3. 模擬實現list 3.1 模擬實現list的構造 3.2 模擬實現list的尾插 3.3 模擬實現迭代器iterator 3.4 模擬實現list的插入刪除 3.5 模擬實現list的析構和clear 3.6 模擬實現list的深拷貝 4. 代碼
??
相信學過string和vector容器后,對于list應該游刃有余了,接下來我將帶大家將list的大致框架過一遍,然后來模擬實現下list。
首先介紹下list,list其實就是C++實現的一個帶頭節點的雙向鏈表,當然如果想了解單鏈表的話,也可以學習forward_list
1. list的構造
構造函數 接口說明 list(size_t n, const size_type& val) 用n個val值來構造list對象 list(Inputiterator first, Inputiterator last) 用迭代器區間構造list對象 list(const list& lt) 用list對象lt來構造list對象
2. list迭代器的常見接口
2.1 list遍歷的迭代器接口
迭代器接口 說明 begin() 指向list對象中的第一個數據 end() 指向list對象最后一個數據的下個位置 rbegin() 指向反向迭代器的第一個數據 rend() 指向反向迭代器的最后一個元素的前一個位置 empty() 判斷list對象是否為空 size() 返回list對象有效數據個數 front() 返回list對象的第一個數據 back() 返回list對象的最后一個數據
2.2 list修改數據的迭代器接口
迭代器接口 說明 assign() 為list容器重新分配內容,并相應修改容器容量大小 push_back() 尾插數據 emplace_back() 尾部插入數據 pop_back() 尾刪數據 push_front() 頭插數據 pop_front() 頭刪數據 emplace_front() 頭部插入數據 insert() 指定位置插入數據 erase() 刪除指定位置數據 swap() 交換兩個list對象里面的數據和size clear() 刪除list里面的所有數據,size置為0
當然assign()也可以使用迭代器進行初始化 需要注意的是,C++實現的迭代器分為3種:單向迭代器、雙向迭代器、隨機迭代器 單向迭代器只支持++不支持 - -
雙向迭代器支持++和 - -
隨機迭代器只支持++、 - - 、+ 、 -
恰好list模版實現時就是雙向迭代器
push_back()
和emplace_back()
的區別
pop_back()、pop_front()刪除數據
由于list模版中沒有實現find函數查找,因此需要調用算法庫實現的find()函數模版
2.3 list排序、逆序、合并相關操作的成員函數
迭代器接口 說明 splice 將另一個列表(或同一列表的另一個部分)的元素移動到當前列表的指定位置 remove 刪除容器中等于val值的元素 reverse 將list對象的數據逆序排列 merge 合并 sort 排序(使用的歸并排序) unique 刪除list中重復且相鄰的數據,只保留一個
list里面的remove(const value_type& val)
會刪除list對象中所有等于val值的數據
其實這里有個疑問需要解釋下:C++算法庫里面實現有sort()排序模版,為什么這里還要實現sort呢? 這是因為算法庫實現的sort容器是隨機迭代器版本,對于鏈表來說不支持隨機訪問,只能從頭遍歷,因此list實現了一個sort排序的接口,主要利用歸并排序可以很好的解決這個問題
merge接口:用來合并兩個list對象,并且會將合并的list對象置size為空 list中merge使用時,需要注意兩個list是已經排好序的,否則無法使用
splice接口:在給定的迭代器位置插入list對象
3. 模擬實現list
3.1 模擬實現list的構造
3.2 模擬實現list的尾插
3.3 模擬實現迭代器iterator
list是雙向鏈表,鏈表的每個節點都是單獨存在的,因此鏈表不是連續的物理空間 list的每個節點都保存了指向上個節點和下個節點的指針,分別為prev、next,另外每個節點還存儲了數據data 由于list不是連續的物理空間,實現list時就不能使用typedef T* iterator
這樣的方式,因為迭代器++時找不到下個節點的位置 由于迭代器指的是具有像指針一樣行為的容器,那么就可以將迭代器單獨封裝為一個類
鏈表的結構圖
list除了存儲一些內置類型的數據,有時還會存儲自定義類型的數據 而自定義類型的對象往往不支持流插入操作,這就回導致迭代器訪問時出現報錯
template < class T >
struct list_const_iterator
{ typedef list_node< T> Node; Node* node; list_const_iterator ( Node* _node) : node ( _node) { } const T& operator * ( ) { return node-> data; } const T* operator -> ( ) { return & node-> data; } list_const_iterator< T> & operator ++ ( ) { node = node-> next; return * this ; } bool operator != ( const list_const_iterator< T> & it) { return node != it. node; } bool operator == ( const list_const_iterator< T> & it) { return node == it. node; }
} ;
const修飾的迭代器本身可以修改,但是指向的內容是不可以修改的 const迭代器使用
合并普通迭代器和const修飾的迭代器,使用迭代器模版
前置++是先++后再使用,因此是++走向下個節點后返回自身的引用;而后置++是先使用再++,因此是先返回自身再走向下個節點,需要創建臨時對象tmp來保存節點更改前的迭代器 這里需要注意的是,為了區分前后置++,默認給后置++的形參參數為int,這里int僅做區分使用。由于前置++不需要創建臨時對象,因此對比之下更高效
3.4 模擬實現list的插入刪除
insert(指定位置)插入和erase(指定位置)刪除
3.5 模擬實現list的析構和clear
3.6 模擬實現list的深拷貝
4. 代碼
# pragma once # include <iostream>
# include <algorithm>
# include <list>
# include <vector>
# include <assert.h>
using namespace std; namespace TT
{ template < class T > struct list_node { list_node< T> * prev; list_node< T> * next; T data; list_node ( const T& x = T ( ) ) : prev ( nullptr ) , next ( nullptr ) , data ( x) { } } ; template < class T , class Ref , class Ptr > struct list_iterator { typedef list_node< T> Node; typedef list_iterator< T, Ref, Ptr> Self; Node* node; list_iterator ( Node* _node) : node ( _node) { } Ref operator * ( ) { return node-> data; } Ptr operator -> ( ) { return & node-> data; } Self& operator ++ ( ) { node = node-> next; return * this ; } Self operator ++ ( int ) { Self tmp ( * this ) ; node = node-> next; return tmp; } Self& operator -- ( ) { node = node-> prev; return * this ; } Self operator -- ( int ) { Self tmp ( * this ) ; node = node-> prev; return tmp; } bool operator != ( const Self& it) { return node != it. node; } bool operator == ( const Self& it) { return node == it. node; } } ; template < class T > class list { typedef list_node< T> Node; public : typedef list_iterator< T, T& , T* > iterator; typedef list_iterator< T, const T& , const T* > const_iterator; iterator begin ( ) { return iterator ( head-> next) ; } iterator end ( ) { return iterator ( head) ; } const_iterator begin ( ) const { return const_iterator ( head-> next) ; } const_iterator end ( ) const { return const_iterator ( head) ; } size_t size ( ) { return _size; } size_t size ( ) const { return _size; } void empty_init ( ) { head = new Node; head-> prev = head; head-> next = head; _size = 0 ; } list ( ) { empty_init ( ) ; } list ( initializer_list< T> lt) { empty_init ( ) ; for ( auto & e : lt) { push_back ( e) ; } } list ( const list< T> & lt) { empty_init ( ) ; for ( auto e : lt) { push_back ( e) ; } } void swap ( const list< T> & lt) { std:: swap ( head, lt. head) ; std:: swap ( _size, lt. _size) ; } list< T> & operator = ( const list< T> lt) { swap ( lt) ; return * this ; } void push_back ( const T& x) { insert ( end ( ) , x) ; } void push_front ( const T& x) { insert ( begin ( ) , x) ; } void pop_front ( ) { erase ( begin ( ) ) ; } void pop_back ( ) { erase ( -- end ( ) ) ; } void insert ( iterator pos, const T& x) { Node* cur = pos. node; Node* prev = cur-> prev; Node* newnode = new Node ( x) ; prev-> next = newnode; newnode-> prev = prev; newnode-> next = cur; cur-> prev = newnode; ++ _size; } iterator erase ( iterator pos) { assert ( pos != end ( ) ) ; Node* cur = pos. node; Node* PrevNode = cur-> prev; Node* NextNode = cur-> next; PrevNode-> next = NextNode; NextNode-> prev = PrevNode; delete cur; -- _size; return iterator ( NextNode) ; } ~ list ( ) { clear ( ) ; delete head; head = nullptr ; } void clear ( ) { iterator it = begin ( ) ; while ( it != end ( ) ) { it = erase ( it) ; } } private : Node* head; size_t _size; } ;
}
# include "list.h"
# include <algorithm> struct A
{ A ( int a1 = 1 , int a2 = 1 ) : _a1 ( a1) , _a2 ( a2) { } int _a1; int _a2;
} ; void list_test1 ( )
{ TT:: list< int > lt1; lt1. push_back ( 1 ) ; lt1. push_back ( 2 ) ; lt1. push_back ( 3 ) ; lt1. push_back ( 4 ) ; TT:: list< int > :: iterator it = lt1. begin ( ) ; while ( it != lt1. end ( ) ) { cout << * it << " " ; ++ it; } cout << endl;
} void Print ( const TT:: list< A> & lt)
{ TT:: list< A> :: const_iterator it = lt. begin ( ) ; while ( it != lt. end ( ) ) { cout << ( * it) . _a1 << " " << ( * it) . _a2 << endl; ; ++ it; } cout << endl;
} void list_test2 ( )
{ TT:: list< A> lt1; lt1. push_back ( { 1 , 1 } ) ; lt1. push_back ( { 2 , 2 } ) ; lt1. push_back ( { 3 , 3 } ) ; lt1. push_back ( { 4 , 4 } ) ; TT:: list< A> :: iterator it1 = lt1. begin ( ) ; while ( it1 != lt1. end ( ) ) { it1-> _a1 += 1 ; cout << it1-> _a1 << ":" << it1-> _a2 << endl; ++ it1; } cout << endl; Print ( lt1) ;
} void list_test3 ( )
{ TT:: list< int > lt1; lt1. push_back ( 1 ) ; lt1. push_back ( 2 ) ; lt1. push_back ( 3 ) ; lt1. push_back ( 4 ) ; for ( auto e : lt1) { cout << e << " " ; } cout << endl; lt1. push_back ( 78 ) ; lt1. push_back ( 99 ) ; lt1. push_front ( 100 ) ; lt1. push_front ( 300 ) ; for ( auto e : lt1) { cout << e << " " ; } cout << endl; lt1. pop_back ( ) ; lt1. pop_front ( ) ; for ( auto e : lt1) { cout << e << " " ; } cout << endl;
} void test_list4 ( )
{ TT:: list< int > lt1; lt1. push_back ( 1 ) ; lt1. push_back ( 2 ) ; lt1. push_back ( 3 ) ; lt1. push_back ( 4 ) ; for ( auto e : lt1) { cout << e << " " ; } cout << endl; TT:: list< int > lt2 ( lt1) ; lt1. clear ( ) ; for ( auto e : lt1) { cout << e << " " ; } cout << endl; for ( auto e : lt2) { cout << e << " " ; } cout << endl; TT:: list< int > lt3 = lt2; for ( auto e : lt3) { cout << e << " " ; } cout << endl; TT:: list< int > lt4 = { 10 , 20 , 30 , 40 } ; for ( auto e : lt4) { cout << e << " " ; } cout << endl;
} int main ( )
{ test_list4 ( ) ; return 0 ;
}