• <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>

            KISS(Keep It Simple, Standard)

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              10 Posts :: 0 Stories :: 24 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(10)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            #ifndef _DOUBLE_H_
            #define _DOUBLE_H_

            template<class T>
            class Double;

            template<class T>
            class DoubleNode
            {
             friend class Double<T>;
            private:
             T data;
             DoubleNode<T> *pre;
             DoubleNode<T> *next;
            };

            template<class T>
            class Double
            {
             public:
              Double();//{head=end=NULL;}
              ~Double();
              void Erase();
              void reverse();
              int GetLength()const;
              bool IsEmpty()const;
              bool Find(int k, T& x)const;
              int Search(T& x)const;
              Double<T>& Delete(int k, T& x);
              Double<T>& Insert(int k, const T& x);
              void output(ostream& out)const;
              friend ostream& operator << (ostream& out, const Double<T>& x);
             private:
              DoubleNode<T> *head;
              DoubleNode<T> *end;
              int length;
            };

            template<class T>
            Double<T>::Double()
            {
             head = new DoubleNode<T>;
             end = new DoubleNode<T>;
             head->pre = NULL;
             head->next = end;
             end->pre = head;
             end->next = NULL;

             length = 0;
            }

            template<class T>
            Double<T>::~Double()
            {
             Erase();
            }

            template<class T>
            void Double<T>::Erase()
            {
             DoubleNode<T> *current = head;
             while (current)
             {
              head = head->next;
              delete current;
              current = head;
             }
             length = 0;
            }

            template<class T>
            int Double<T>::GetLength()const
            {
             return length;
            }

            template<class T>
            bool Double<T>::IsEmpty()const
            {
             return length == 0;
            }

            template<class T>
            bool Double<T>::Find(int k, T& x)const
            {

             if (length == 0)
             {
              throw exception("DoubleNode is empty!");
             }
             else if(k<1 || k>length)
             {
              throw exception("no find the position of k");
             }

             DoubleNode<T> *current = head->next;
             for (int i=1; (i<k)&&current; ++i)
             {
              current = current->next;
             }

             if (current)
             {
              x = current->data;
              return true;
             }

             return false;
            }


            template<class T>
            int Double<T>::Search(T& x)const
            {
             int nIndex = 1;
             DoubleNode<T> *current = head->next;
             while (current && current->data != x)
             {
              ++nIndex;
              current = current->next;
             }

             if (current)
             {
              return nIndex;
             }

             return -1;
            }

            template<class T>
            Double<T>& Double<T>::Delete(int k, T& x)
            {
             if (length == 0)
             {
              throw exception("DoubleNode is empty!");
             }
             else if(k<1 || k>length)
             {
              throw exception("no find the position of k, so can't delete!");
             }

             DoubleNode<T> *current = head->next; 
             for (int i=1; (i<k)&&current; ++i)
             {
              current = current->next;
             }

             DoubleNode<T> * p = current;
             current->pre->next = current->next;
             current->next->pre = current->pre;

             x = p->data;
             delete p;
             p = NULL;
             --length;

             return *this;
            }


            template<class T>
            Double<T>& Double<T>::Insert(int k, const T& x)
            {
             if (k>=0 && k<= length)
             {
              DoubleNode<T> *newNode = new DoubleNode<T>;
              newNode->data = x;

              DoubleNode<T> *current = head;
              for (int i=0; i<k; ++i)
              {
               current = current->next;
              }

              newNode->pre = current;
              newNode->next = current->next;
              current->next->pre = newNode;
              current->next = newNode;
              
              
              ++length;
             }
             else
             {
              throw exception("no find the position of k, so can't insert!");
             }

             return *this;
            }

            template<class T>
            void Double<T>::output(ostream& out)const
            {
             DoubleNode<T> *current = head->next;
             while (current!=end)
             {
              out << current->data << " ";
              current = current->next;
             }
            }

            template<class T>
            ostream& operator<< (ostream& out, const Double<T>& x)
            {
             x.output(out);
             return out;
            }

            template<class T>
            void Double<T>::reverse()
            {
             DoubleNode<T> *p1 = head;
             DoubleNode<T> *p2 = NULL;
             DoubleNode<T> *pNode;

             while (p1 != NULL)
             {
              pNode = p1;
              pNode->pre = p1->next;
              p1 = p1->next;
              pNode->next = p2;
              p2 = pNode;
             }

             end = head;
             head = p2;
            }

            #endif

            以上為雙鏈表的基本操作,代碼已經測試過了,可以直接用!
            其中,head. end在構造函數時,New了兩個對象,是為了Insert 和 Delete操作的方便!
            更好的方式是:把指針和數據分開,這樣head,end就可以節省存貯空間了!
            方式如下:
            //指針數據部分(后續指針和前驅指針)
            struct Node_base
            {
             Node_base *next;
             Node_base *pre;
            };

            //添加實際數據部分
            template <class T>
            struct Node : public Node_base
            {
             T m_data;
            };

            posted on 2007-08-24 17:06 QUIRE-0216 閱讀(1403) 評論(4)  編輯 收藏 引用 所屬分類: C++

            Feedback

            # re: 雙鏈表的代碼實現 2007-08-29 11:04 帥哥
            歡迎來##公司面試的人,認真閱讀!  回復  更多評論
              

            # re: 雙鏈表的代碼實現 2007-08-29 11:13 胡錦濤說他同意毛主席的觀點!
            大師搞個java版的鏈表吧,C++就像一攤屎一樣!  回復  更多評論
              

            # re: 雙鏈表的代碼實現 2007-09-02 13:43 螞蟻終結者
            雙鏈表還是STL的list比較棒  回復  更多評論
              

            # re: 雙鏈表的代碼實現 2011-03-17 14:51 leiwei
            樓主,你這代碼有問題啊,  回復  更多評論
              

            国产精品无码久久综合| 久久伊人色| 大蕉久久伊人中文字幕| 看全色黄大色大片免费久久久| 久久综合精品国产一区二区三区| 欧美精品乱码99久久蜜桃| 久久精品国产亚洲av麻豆小说| 99久久人人爽亚洲精品美女| 久久久久久伊人高潮影院| 91久久精品国产成人久久| 久久亚洲AV无码精品色午夜麻豆 | 无码人妻精品一区二区三区久久 | 99热热久久这里只有精品68| 合区精品久久久中文字幕一区| 久久久精品国产sm调教网站| 四虎国产精品成人免费久久| 久久国产精品99国产精| 免费久久人人爽人人爽av| 一本大道加勒比久久综合| 色8久久人人97超碰香蕉987| 国产精品久久久久久久人人看| 一级做a爰片久久毛片人呢| 嫩草伊人久久精品少妇AV| 99久久精品国产一区二区| 狠狠色综合久久久久尤物| 久久―日本道色综合久久| 久久久老熟女一区二区三区| 久久亚洲私人国产精品vA| 无遮挡粉嫩小泬久久久久久久| 亚洲欧美久久久久9999| 国产精品99久久久久久www| 免费精品99久久国产综合精品| 99国产欧美精品久久久蜜芽| 久久久久久国产精品免费无码| 亚洲午夜久久久影院| 久久夜色精品国产噜噜噜亚洲AV| 7777精品久久久大香线蕉| 性欧美丰满熟妇XXXX性久久久 | 久久综合噜噜激激的五月天| 亚洲va中文字幕无码久久| 中文国产成人精品久久不卡|