设为首页 加入收藏

TOP

自己写的c++双向链表(一)
2015-11-21 00:57:15 来源: 作者: 【 】 浏览:3
Tags:自己 双向

尝试使用lambda和模板写一个链表

?

?

#include stdafx.h
template
  
   
struct Node{
	T Value;
	struct Node * pNext;
	struct Node * pPrev;
};
template
   
     class List{ private: Node
    
      * m_pHead; int m_len; Node
     
      * List
      
       ::NewNode(Node
       
        * prev, T value); Node
        
         * List
         
          ::DeleteNode(Node
          
           * node, std::function
           
             pAction); void List
            
             ::MoveNext(Node
             
              * last, std::function
              
               *(Node
               
                *)> pAction); Node
                
                 * List
                 
                  ::MoveTo(T value); Node
                  
                   * List
                   
                    ::MoveToAt(int index); public: List(); ~List(); int GetLength(); bool isEmpty(); bool Insert(T value); bool Remove(T value, std::function
                    
                      pAction); bool RemoveAt(int index, std::function
                     
                       pAction); T ElementAt(int index); void Dispose(std::function
                      
                        pAction); void Dispose(); void ActionAsc(std::function
                       
                         pAction); void ActionDesc(std::function
                        
                          pAction); };
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  

#include stdafx.h
#include List.h
template
  
   
List
   
    ::List() { this->m_len = 0; this->m_pHead = NULL; } template
    
      List
     
      ::~List() { if (this->m_len > 0) { this->Dispose(); } } template
      
        Node
       
        * List
        
         ::NewNode(Node
         
          * prev, T value) { Node
          
           * temp = new Node
           
            (); temp->pNext = NULL; temp->pPrev = prev; temp->Value = value; return temp; }; template
            
              Node
             
              * List
              
               ::DeleteNode(Node
               
                * node, std::function
                
                  pAction) { bool headFlag = node == m_pHead; Node
                 
                  * result = NULL; T tempValue = node->Value; node->Value = NULL; if (pAction != NULL) pAction(tempValue); if (this->m_len != 1) { Node
                  
                   * prev = node->pPrev; Node
                   
                    * next = node->pNext; prev->pNext = next; next->pPrev = prev; result = node->pNext; } delete node; this->m_len--; if (headFlag) { m_pHead = result; } return result; } template
                    
                      Node
                     
                      * List
                      
                       ::MoveTo(T value) { if (m_pHead == NULL) return NULL; Node
                       
                        *p = m_pHead; do { if (p->Value == value) { return p; } else{ p = p->pNext; } } while (p == NULL ? false : p != this->m_pHead); return NULL; } template
                        
                          Node
                         
                          * List
                          
                           ::MoveToAt(int index) { if (m_pHead == NULL &&this->m_len <= index) return NULL; Node
                           
                            *p = m_pHead; int tempIndex = -1; do { tempIndex++; if (index == tempIndex) { return p; } else{ p = p->pNext; } } while (p == NULL ? false : p != this->m_pHead); return NULL; } template
                            
                              void List
                             
                              ::MoveNext(Node
                              
                               * last,std::function
                               
                                *(Node
                                
                                 *)> pAction) { if (last == NULL || pAction == NULL) return; Node
                                 
                                  *p = last; do { p = pAction(p); } while (p == NULL ? false : p != last); } template
                                  
                                    int List
                                   
                                    ::GetLength() { return this->m_len; }; template
                                    
                                      bool List
                                     
                                      ::isEmpty() { return this->GetLength() == 0; }; template
                                      
                                        bool List
                                       
                                        ::Insert(T value) { Node
                                        
                                          * temp = this->NewNode(NULL, value); if (this->m_pHead == NULL)//head is empty { temp->pPrev = temp; this->m_pHead = temp; } else { Node
                                         
                                           * p = this->m_pHead; p = p->pPrev; p->pNext = temp; temp->pPrev = p; this->m_pHead->pPrev = temp; } temp->pNext = m_pHead; this->m_len++; return true; }; template
                                          
                                            bool List
                                           
                                            ::Remove(T value, std::function
                                            
                                              pAction) { Node
                                             
                                              *p = this->MoveTo(value); if (p != NULL) { this->DeleteNode(p, pAction); } return p != NULL; }; template
                                              
                                                bool List
                                               
                                                ::RemoveAt(int index, std::function
                                                
                                                  pAction) { Node
                                                 
                                                  *p = this->MoveToAt(index); if (p != NULL) { this->DeleteNode(p, pAction); } return p != NULL; }; template
                                                  
                                                    T List
                                                   
                                                    ::ElementAt(int index) { Node
                                                    
                                                     *p = this->MoveToAt(value); return p != NULL?p->Value:NULL; }; template
                                                     
                                                       void List
                                                      
                                                       ::Dispose(std::function
                                                       
                                                         pAction) { this->MoveNext(this->m_pHead, [=](Node
                                                        
                                                         * p){return this->DeleteNode(p, pAction); }); }; template
                                                         
                                                           void List
                                                          
                                                           ::Dispose() { this->Dispose([](T t){}); } te
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1421 搬寝室 (线性dp 贪心预.. 下一篇codeforces 558D Guess Your Way ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: