使用C++实现的双向链表

不多说,直接上代码。

注意:其中的Exception类请参考我的博客《使用C++实现的单向循环链表》。

#ifndef DDCHAIN_H
#define DDCHAIN_H

#include "Exception.h"
#include 
   
   
    
    

template
    
    
     
     
class DDChain;

template
     
     
      
      
class DDChainNode{
    friend class DDChain
      
      
       
       ;
private:
    T data;
    DDChainNode
       
       
         * left; DDChainNode 
        
          * right; }; template 
         
           class DDChain{ public: DDChain(){leftEnd = rightEnd = 0;} ~DDChain(); int Length() const; bool isEmpty(); bool Find(int k,T& x) const; int Search(const T& x); DDChain 
          
            & Delete(int k,T& x); DDChain 
           
             & Insert(int k,const T& x); void print(); private: DDChainNode 
            
              * leftEnd; DDChainNode 
             
               * rightEnd; }; template 
              
                DDChain 
               
                 ::~DDChain() { DDChainNode 
                
                  * current = leftEnd; while(current){ leftEnd = leftEnd->right; delete current; current = leftEnd; } leftEnd = rightEnd = 0; } template 
                 
                   bool DDChain 
                  
                    ::isEmpty() { return ((leftEnd == rightEnd) && (leftEnd == 0)); } template 
                   
                     int DDChain 
                    
                      ::Length() const { DDChainNode 
                     
                       * current = leftEnd; int index = 0; while(current){ index++; current = current->right; } return index; } template 
                      
                        bool DDChain 
                       
                         ::Find(int k, T &x) const { if(k < 1) throw OutOfBounds(); DDChainNode 
                        
                          * current = leftEnd; for(int i = 1;i < k && current;i++){ current = current->right; } if(!current) return false; x = current->data; return true; } template 
                         
                           int DDChain 
                          
                            ::Search(const T &x) { DDChainNode 
                           
                             * current = leftEnd; int index = 1; while(current && (current->data != x)){ index++; current = current->right; } if(current) return index; else return -1; } template 
                            
                              DDChain 
                             
                               & DDChain 
                              
                                ::Delete(int k, T &x) { if(k < 1) throw OutOfBounds(); DDChainNode 
                               
                                 * current = leftEnd; DDChainNode 
                                
                                  *p = current->left; DDChainNode 
                                 
                                   *q = current->right; for(int i = 1;i < k && current;i++){ p = current; current = current->right; q = current->right; } if(!current) throw OutOfBounds(); if(p == 0 && q == 0){ x = current->data; delete current; leftEnd = rightEnd = 0; return *this; } if(p == 0 && q != 0){ leftEnd = leftEnd->right; x = current->data; delete current; return *this; } if(q == 0 && p != 0){ rightEnd = rightEnd->left; x = current->data; delete current; return *this; } p->right = current->right; q->left = current->left; x = current->data; delete current; return *this; } template 
                                  
                                    DDChain 
                                   
                                     & DDChain 
                                    
                                      ::Insert(int k, const T &x) { if(k < 0) throw OutOfBounds(); DDChainNode 
                                     
                                       * temp = new DDChainNode 
                                      
                                        (); temp->data = x; if((leftEnd == rightEnd) && (leftEnd == 0)){ std::cout << "insert" << std::endl; leftEnd = rightEnd = temp; leftEnd->left = 0; leftEnd->right = 0; return *this; } if(k == 0){ temp->left = 0; temp->right = leftEnd; leftEnd = temp; return *this; } DDChainNode 
                                       
                                         * current = leftEnd; DDChainNode 
                                        
                                          * p = current->right; for(int i = 1;i < k && current;i++){ current = current->right; p = current->right; } if(!current) throw OutOfBounds(); if(current && !p){ rightEnd->right = temp; temp->left = rightEnd; temp->right = 0; rightEnd = temp; return *this; } current->right = temp; temp->right = p; temp->left = current; p->left = temp; return *this; } template 
                                         
                                           void DDChain 
                                          
                                            ::print() { DDChainNode 
                                           
                                             * current = leftEnd; while(current){ std::cout << current->data << " "; current = current->right; } std::cout << std::endl; } #endif // DDCHAIN_H 
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天堂1223

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值