• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            游戲的天空

            堅持不懈每一天

            C++博客 聯系 聚合 管理
              7 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks
            最近開始學習STL,C 標準模板庫,個人根據標準模板庫中的list 自己寫了個list模板,由于作業量較大,只實現了list里的基本功能,給大家分享下,如有更好的意見請加qq350544011,一起討論討論. 代碼如下:List.h文件:
            最近創建了自己的討論群 希望大家能一起加入 群號:45811732
            #ifndef LIST#define LIST#include "stdafx.h"

            /////////////////////////////鏈表類模板的聲明///////////////////////

            template<typename T>

            class CList

            {

            public:  
            //結點 struct Node 
            {    

               T node;

              Node* next; 

            };

             //跌代器 
             class iterator 
            {
                public:  iterator()  
               {     

               };

              ~iterator()  {          };       

               //重載"!=" 
                bool operator != (iterator &it) 
                {   
                     if(m_val != it.m_val)   
                     {   
                         return true;   
                     } 
                     return false;  
               }

              //重載"前++ "  
               iterator operator++ ()  
               {   
                  m_val = m_val->next;

                  return *this;  
               }

              //重載 "后++ "  
               iterator operator++ (int)
             {   
               m_val = m_val->next;

               return *this; 
             }

              //重載*  
            T operator*()  
            {  
                return m_val->node; 
              }

              Node *m_val; 

            private:    

            }; 

             CList();

             ~CList();   

            //在連表尾部添加
             void push_back(const T &_node);

             //在連表的頭部添加 
            void push_front(const T &_node);

             //刪除連表中的最后一個元素 
            void pop_back();

             //刪除連表中的第一個元素 
            void pop_front();

             //返回指向第一個元素的迭代器
             typename CList<T>::iterator begin();

             //返回末尾的迭代器 
            typename CList<T>::iterator end();

             //判斷是否為空
             bool empty(){ if(!m_phead){ return true; } return false; }

             //清空鏈表
             void clear();

            protected:

             private:  

                Node *m_phead;   

              };

             

            ////////////////////////////類模板的函數定義///////////////////////

            template<typename T>
            CList<T>::~CList()

            {

                //刪除鏈表里的所有結點
                if(m_phead)  
               {   
                  Node* temp = m_phead;

                 while(temp->next!=NULL) 
                  {  
                         Node* temp1 = temp;          

                        temp = temp->next;

                        delete temp1;

                     temp1 = NULL;  

                  }

                 m_phead = NULL;

                }

            }

            template<typename T>
             CList<T>::CList()

            m_phead = NULL;

            }

            template<typename T> 
            void CList<T>::push_back(const T &_node)

             //如果連表為空
             if(empty()) 
            {   
               Node* tempnode = new Node;

              tempnode->node =  _node;

              tempnode->next = NULL;

              m_phead = tempnode; 
             
             } 
            //如果連表不為空 
            else 
            {   

               Node *temp = m_phead;

                 while(temp->next!=NULL) 
                {   

                  temp = temp->next;  
                  }    
             
               Node* tempnode = new Node;    
               
               tempnode->node = _node;     
               
               tempnode->next = NULL;

              temp->next = tempnode;  
              
            }  

            }

             template<typename T>

            void CList<T>::push_front(const T &_node)

               if(empty()) 
               {     
                     Node* tempnode = new Node;

                    tempnode->node =  _node;

                    tempnode->next = NULL;

                    m_phead = tempnode;   
               } 
               else 
               { 
                   Node* tempnode = new Node;

                    tempnode->node = _node;

                    tempnode->next = m_phead->next;     m_phead = tempnode;

                }

            }

            template<typename T>void CList<T>::pop_back()

               if(empty())
                {  
                     return ; 
                } 
                else
                { 
                   Node* temp = m_phead;

                    if(!temp->next)  
                     {  

                      delete temp;

                  m_phead = NULL; 
                   }  
                   else  
                  {   
                     while(temp->next->next!=NULL)   
                     {    
                        temp = temp->next; 
                       }

                     delete temp->next;

               temp->next = NULL; 
                }   
                }
               }

            template<typename T>void CList<T>::pop_front()
             { 
               if(empty()) 
               {  
                  return; 
               } 
               else 
               {  
                     Node* temp = m_phead;

                    m_phead = m_phead->next;

                    delete temp;

                    temp = NULL; 
               }

            }

             template<typename T>typename CList<T>::iterator CList<T>::begin()
            {  
               iterator tempit;

               tempit.m_val = m_phead;

              return tempit;
            }

             template<typename T>typename CList<T>::iterator CList<T>::end()
            {  
               //如何去釋放?  
               iterator tempit  ;

              Node* tempnode1 = m_phead;

              while(tempnode1!=NULL)  
                  tempnode1 = tempnode1->next;

              tempit.m_val = tempnode1;

              return tempit;

            }

            template<typename T>void CList<T>::clear()

               //刪除鏈表里的所有結點 if(m_phead)
                {  
                  Node* temp = m_phead;

                 while(temp->next!=NULL)  
                  {   
                        Node* temp1 = temp;         
                         
                        temp = temp->next;

                        delete temp1;

                        temp1 = NULL;
              }

                    m_phead = NULL;  
               }

            }

            #endif
            posted on 2012-03-24 06:09 GLpro 閱讀(1222) 評論(1)  編輯 收藏 引用 所屬分類: C++基礎學習筆記

            Feedback

            # re: 自己實現了STL里的list部分功能 2012-03-25 09:49 tb
            相當的不錯啊   回復  更多評論
              

            久久香蕉超碰97国产精品| 久久精品国产亚洲精品| 久久久久一级精品亚洲国产成人综合AV区 | 少妇熟女久久综合网色欲| 日本五月天婷久久网站| 久久婷婷五月综合色高清| 99国产精品久久久久久久成人热| 99久久亚洲综合精品成人| 久久久久亚洲av成人无码电影 | 亚洲AV日韩精品久久久久久| 2021久久精品国产99国产精品| 中文字幕成人精品久久不卡| 久久亚洲熟女cc98cm| 精品久久久久久中文字幕| 亚洲国产综合久久天堂| 青青青伊人色综合久久| 国产精品99久久久精品无码| 94久久国产乱子伦精品免费| 国产亚洲美女精品久久久2020| 久久久99精品一区二区| 俺来也俺去啦久久综合网| 无码人妻久久一区二区三区蜜桃| 国产精品九九久久免费视频 | 亚洲精品乱码久久久久久按摩| 国产国产成人久久精品 | 久久福利青草精品资源站| 97久久国产露脸精品国产| 久久精品国产亚洲7777| 久久精品国产精品青草app| 久久无码人妻一区二区三区午夜| 久久亚洲国产最新网站| 亚洲?V乱码久久精品蜜桃| 久久久久无码精品国产app| 日韩一区二区久久久久久| 国内精品久久久久久99| 久久亚洲国产成人精品性色| 久久国产免费直播| 久久人人爽人人爽人人片AV东京热| 亚洲伊人久久综合中文成人网| 欧美色综合久久久久久| 亚洲国产成人乱码精品女人久久久不卡|