資料結構篇-單鏈表

來源:互聯網
上載者:User

先自己寫了一個簡單但是代碼寫的挺差的單鏈表:

#include"mymemory.h"#include"myconstruct.h"#include<iostream>//簡單單鏈表template<typename T>struct _slist_node {T data;_slist_node* next;};//既然是簡單鏈表,我們應該使用new分配記憶體,不過,我覺得我們確實應該使用記憶體池template<typename T,typename Alloc=alloc>class slist{private:typedef _slist_node<T>  list_node;typedef simple_alloc<list_node,Alloc> node_allocator;private://資料成員list_node* first;list_node* last;int len;public:slist():first(0),last(0),len(0){ }~slist(){clear();}size_t size() const{ return len;}public:list_node* get_front(){return first;}list_node* get_end(){return last;}list_node* find(const T& e);void insert_front(const T& e);void insert_back(const T& e);void pop_front();void insert(list_node* p,const T& e);//在結點p之後插入void remove(const T& e);//在鏈表中刪除值為e的結點void display(std::ostream& os);private:void clear();//在解構函式中調用,釋放記憶體void destroy_node(list_node* node)//釋放結點{destroy(&(node->data));node_allocator::deallocate(node);}list_node* get_node(const T& e=T());//輔助程式,以e為值構造結點public:void concatenate(slist& L);//合并兩個表void swap(slist& L);//兩個鏈表交換};//---------------------------------------------------------------template<typename T,typename Alloc>typename slist<T,Alloc>::list_node* slist<T,Alloc>::find(const T& e){list_node* p=first;while(p!=NULL){if(p->data==e){return p;}p=p->next;}return NULL;}//-------------------------------------------------------------------template<typename T,typename Alloc>typename slist<T,Alloc>::list_node*  slist<T,Alloc>::get_node(const T& e){list_node* tmp=node_allocator::allocate();tmp->next=NULL;tmp->data=e;return tmp;}//------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::insert_back(const T& e){if(first){last->next=get_node(e);last=last->next;++len;}else{first=last=get_node(e);++len;}}//--------------------------------------------------------------------template<typename T,typename Alloc>void  slist<T,Alloc>::insert_front(const T& e){if(first){list_node* tmp=get_node(e);tmp->next=first;first=tmp;++len;}else{first=last=get_node(e);++len;}}//-------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::insert(list_node* p,const T& e){list_node* next=p->next;list_node* tmp=get_node(e);p->next=tmp;tmp->next=next;++len;}//---------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::clear(){list_node* p=first;while(p){list_node* tmp=p;p=p->next;destroy_node(tmp);}first=last=0;len=0;}//-------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::swap(slist& L){list_node* tmp_first=first;list_node* tmp_last=last;size_t tmp_len=len;first=L.first;last=L.last;len=L.len;L.first=tmp_first;L.last=tmp_last;L.len=tmp_len;}//-------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::concatenate(slist& L){if(first){last->next=L.first;last=L.last;}else{first=L.first;last=L.last;}len+=L.len;L.first=L.last=0;L.len=0;}//-----------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::pop_front(){list_node* tmp=first->next;destroy_node(first);first=tmp;--len;}//-----------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::remove(const T& e){list_node* prev=0;list_node* p=first;while(p){if(p->data==e){prev->next=p->next;destroy_node(p);--len;p=prev->next;}else{prev=p;p=p->next;}}}//----------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::display(std::ostream& os){list_node* p=first;for(;p!=NULL;p=p->next){os<<p->data<<'\n';}os<<std::endl;}//--------------------------------------------------------------------------

然後附上stl版的單鏈表:

//單鏈表的stl實現,與迭代器配合#include"mymemory.h"#include"myconstruct.h"template<typename T>struct __slist_node{__slist_node* next;T data;};template<typename T,typename Ref,typename Ptr>struct __slist_iterator{typedef  forward_iterator_tag iterator_category;typedef  __slist_iterator<T,T&,T*>   iterator;typedef __slist_iterator<T,const T&,const T*> const_iterator;typedef __slist_iterator<T,Ref,Ptr> self;typedef T value_type;typedef Ptr  pointer;typedef Ref  reference;typedef __slist_node<T>  list_node;typedef size_t size_type;typedef ptrdiff_t difference_type;list_node* node;__slist_iterator(list_node* x):node(x){ }__slist_iterator():node(0){}__slist_iterator(const iterator& x):node(x.node){}reference operator*() const{return node->data;}pointer   operator->() const {return &(operator*());}self& operator++(){node=node->next;return *this;}self   operator++(int){self tmp=*this;node=node->next;return tmp;}bool operator==(const __slist_iterator& x) const {return node=x.node;}bool operator!=(const __slist_iterator& x) const{return node!=x.node;}};template<typename T,typename Alloc=alloc>class slist{public:typedef T value_type;typedef value_type* pointer;typedef const value_type* const_pointer;typedef value_type&  reference;typedef const value_type& const_reference;typedef size_t  size_type;typedef ptrdiff_t  difference_type;typedef __slist_iterator<T,T&,T*> iterator;//傳遞const引用和指標,對於返回引用和指標的函數,都返回的是const版本的typedef __slist_iterator<T,const T& ,const T*> const_iterator;private://------------記憶體管理工具-----------------------------------------------------------typedef __slist_node<T>  list_node;typedef  simple_alloc<list_node,Alloc> node_allocator;//配置一個節點並傳回static list_node* get_node(){return node_allocator::allocate();}//釋放一個節點static list_node* put_node(list_node* p){node_allocator::deallocate();}//產生一個節點,產生即配置空間和構造元素static list_node* create_node(const T& value);//銷毀一個節點,銷毀即析構元素和釋放記憶體static void destroy_node(list_node* node){destroy(&node->data);node_allocator::deallocate(node);}//-------------------------結束-------------------------------------------------------private://這裡依然採用head為哨兵,head的next為list的第一個元素,當空鏈表時head的next指向自己;//鏈表的最後一個元素的next指向head,即head為last迭代器list_node*   head;public:slist(){head=get_node();head->next=head;}~slist(){clear();}public:/*------------------------------------介面--------------------------------------------*/iterator begin(){return iterator(head->next);}iterator end(){return iterator(head);}inline size_type size()const;bool empty() const {return head->next==head;}inline void swap(slist& L);//兩個list交換reference front(){return head->next->data;}//取頭部元素void push_front(const value_type& x);    void pop_front();void  clear();//清空鏈表void insert(iterator p,const value_type& x);//在p之後插入值為x的節點//由於有了迭代器,所以很多函數可以用泛型演算法,如find。。。/*----------------------------------結束------------------------------------------*/};//---------------------------------------------------------------------------------template<typename T,typename Alloc>typename slist<T,Alloc>::list_node*  slist<T,Alloc>::create_node(const value_type& x){list_node* node=node_allocator::allocate();//產生空間try{//構造元素,如果構造失敗,就析構;construct(&node->data,x);node->next=0;}catch(...){node_allocator::deallocate(node);}return node;}//-------------------------------------------------------------------------------------template<typename T,typename Alloc>inline typename slist<T,Alloc>::size_type   slist<T,Alloc>::size() const{size_type result=0;for(list_node* p=head->next;p!=head;p=p->next){++result;}return result;}//-------------------------------------------------------------------------------------template<typename T,typename Alloc>inline void   slist<T,Alloc>::swap( slist<T,Alloc>& L){list_node* tmp=head;head=L.head;L.head=tmp;}//------------------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::push_front(const value_type& x){list_node* next=head->next;list_node* cur=create_node(x);head->next=cur;cur->next=next;}//--------------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::pop_front(){list_node* cur=head->next;head->next=cur->next;destroy_node(cur);}//----------------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::insert(iterator p,const value_type& x){list_node* cur=create_node(x);list_node* next=p.node->next;p.node->next=cur;cur->next=next;}//-----------------------------------------------------------------------------template<typename T,typename Alloc>void slist<T,Alloc>::clear(){list_node* next=0;list_node* p=head->next;while(p!=head){next=p->next;destroy_node(p);p=next;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.