• <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++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              10 Posts :: 0 Stories :: 24 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(10)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            #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

            以上為雙鏈表的基本操作,代碼已經(jīng)測(cè)試過(guò)了,可以直接用!
            其中,head. end在構(gòu)造函數(shù)時(shí),New了兩個(gè)對(duì)象,是為了Insert 和 Delete操作的方便!
            更好的方式是:把指針和數(shù)據(jù)分開(kāi),這樣head,end就可以節(jié)省存貯空間了!
            方式如下:
            //指針數(shù)據(jù)部分(后續(xù)指針和前驅(qū)指針)
            struct Node_base
            {
             Node_base *next;
             Node_base *pre;
            };

            //添加實(shí)際數(shù)據(jù)部分
            template <class T>
            struct Node : public Node_base
            {
             T m_data;
            };

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

            Feedback

            # re: 雙鏈表的代碼實(shí)現(xiàn) 2007-08-29 11:04 帥哥
            歡迎來(lái)##公司面試的人,認(rèn)真閱讀!  回復(fù)  更多評(píng)論
              

            # re: 雙鏈表的代碼實(shí)現(xiàn) 2007-08-29 11:13 胡錦濤說(shuō)他同意毛主席的觀點(diǎn)!
            大師搞個(gè)java版的鏈表吧,C++就像一攤屎一樣!  回復(fù)  更多評(píng)論
              

            # re: 雙鏈表的代碼實(shí)現(xiàn) 2007-09-02 13:43 螞蟻終結(jié)者
            雙鏈表還是STL的list比較棒  回復(fù)  更多評(píng)論
              

            # re: 雙鏈表的代碼實(shí)現(xiàn) 2011-03-17 14:51 leiwei
            樓主,你這代碼有問(wèn)題啊,  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            国产99久久久国产精品~~牛 | 久久婷婷五月综合97色| 色综合久久久久综合体桃花网 | 久久国产亚洲高清观看| segui久久国产精品| 97久久国产露脸精品国产| 69久久夜色精品国产69| 深夜久久AAAAA级毛片免费看 | 国产精品久久免费| 少妇久久久久久被弄到高潮| 久久久久人妻一区精品性色av| 久久99精品久久久久久水蜜桃| 欧美va久久久噜噜噜久久| 99久久精品免费看国产| 久久国产精品久久久| 国产成人精品综合久久久久| 久久国产成人| 久久精品亚洲精品国产色婷| 精产国品久久一二三产区区别| 国产成人无码久久久精品一| 久久国产色av免费看| 午夜精品久久久内射近拍高清 | 久久精品极品盛宴观看| 久久精品国产WWW456C0M| 久久午夜电影网| 国产成人精品久久| 国产免费久久精品99re丫y| 日本加勒比久久精品| 精品久久久久久无码人妻热| 麻豆精品久久精品色综合| 情人伊人久久综合亚洲| 久久久久久综合一区中文字幕| 国产精品一区二区久久不卡| 国产成年无码久久久久毛片| 久久久国产精品亚洲一区| 久久99国产乱子伦精品免费| 精品熟女少妇a∨免费久久| 精品无码久久久久国产| 久久精品国产精品青草| 91精品日韩人妻无码久久不卡| 久久99精品久久久久久水蜜桃|