• <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>
            隨筆 - 8  文章 - 26  trackbacks - 0
            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            相冊

            C++語言

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            1
              1#ifndef LIST_H
              2#define LIST_H
              3#include <iostream>
              4//鏈表節點
              5template<class T>
              6class ListNode
              7{
              8public:
              9    ListNode(ListNode<T>* nextnode=0,const T &val=T())
             10    {
             11    data=val;
             12    next=nextnode;
             13    }

             14public:
             15T data;
             16    ListNode<T> *next;
             17
             18}
            ;
             19
             20//鏈表實現
             21template<class T>
             22class List
             23{
             24
             25public:
             26    List();
             27    virtual ~List();
             28    void Insert_Front(const T &e);//向表頭插入節點
             29    void Insert_End(const T &e);//向表尾插入節點
             30    ListNode<T>* Find(const T &e);//查找指定節點
             31    bool Delete(const T &e);//刪除指定節點
             32    List<T>& Delete_All();//刪除除了頭結點以外的所有節點
             33    bool IsEmpty();//測試鏈表是否為空
             34    bool Size() const {return size;}//返回鏈表中的節點數目
             35    void OutPut();
             36
             37private:
             38    ListNode<T>*front,*rear,*head;//頭指針與尾指針
             39    int size;//鏈表元素節點數目
             40}
            ;
             41
             42
             43
             44
             45//---------------------------------------------------------------------
             46template<class T>
             47List<T>::~List()
             48{
             49Delete_All();
             50delete head;
             51
             52}

             53//---------------------------------------------------------------------
             54template<class T>
             55List<T>::List()
             56{
             57//構造頭接點
             58head=new ListNode<T>();
             59front=rear=head;
             60head->next=head;
             61}

             62
             63//---------------------------------------------------------------------
             64template<class T>
             65void List<T>::Insert_Front(const T &e)
             66{
             67    ListNode<T> *NewNode=new ListNode<T>(0,e);
             68if(front->next==head)//如果鏈表為空
             69{
             70
             71front->next=NewNode;
             72NewNode->next=head;
             73rear=NewNode;
             74}

             75else//鏈表不為空
             76{
             77NewNode->next=front->next;
             78front->next=NewNode;
             79}

             80++size;
             81}

             82//---------------------------------------------------------------------
             83template<class T>
             84void List<T>::Insert_End(const T &e)
             85{
             86    ListNode<T> *NewNode=new ListNode<T>(0,e);
             87if(front->next==head)//如果鏈表為空
             88{
             89
             90front->next=NewNode;
             91NewNode->next=head;
             92rear=NewNode;
             93}

             94else//鏈表不為空
             95{
             96rear->next=NewNode;
             97NewNode->next=head;
             98rear=NewNode;
             99
            100}

            101++size;
            102}

            103//---------------------------------------------------------------------
            104template<class T>
            105ListNode<T>* List<T>::Find(const T &e)
            106{
            107head->data=e;
            108ListNode<T> *move=front->next;
            109while(move->data!=e)
            110{
            111move=move->next;
            112}

            113
            114if(move==front) return NULL;
            115else
            116return move;
            117}

            118
            119//---------------------------------------------------------------------
            120template<class T>
            121bool List<T>::Delete(const T &e)
            122{
            123head->data=e;
            124ListNode<T> *move=front->next;
            125ListNode<T> *pmove=head;
            126while(move->data!=e)
            127{
            128    pmove=move;
            129move=move->next;
            130}

            131if(move==head) return false;//未找到節點
            132pmove->next=move->next;
            133if(move==rear)//如果為尾節點則修改尾指針
            134rear=pmove;
            135delete move;
            136return true;
            137}

            138
            139//---------------------------------------------------------------------
            140template<class T>
            141void List<T>::OutPut()
            142{
            143ListNode<T> *move=front->next;
            144while(move!=head)
            145{
            146cout<<move->data<<" ";
            147move=move->next;
            148}

            149cout<<endl;
            150}

            151//---------------------------------------------------------------------
            152template<class T>
            153List<T>& List<T>::Delete_All()
            154{
            155ListNode<T> *movenext,*move=front->next;
            156
            157while(move!=head)
            158{
            159movenext=move->next;
            160delete move;
            161move=movenext;
            162}

            163front=rear=head;
            164head->next=head;
            165
            166return *this;
            167}

            168//---------------------------------------------------------------------
            169template<class T>
            170bool List<T>::IsEmpty()
            171{
            172
            173if(head->next=head) return true;
            174else 
            175return false;
            176}

            177#endif
            posted on 2008-09-18 20:54 楊彬彬 閱讀(1760) 評論(0)  編輯 收藏 引用 所屬分類: 數據結構
            狠狠精品干练久久久无码中文字幕| 久久精品国产亚洲av日韩| 亚洲国产精品嫩草影院久久| 国产成人精品久久综合 | 国产成年无码久久久免费| 亚洲国产精品一区二区三区久久| 香蕉久久久久久狠狠色| 无码人妻精品一区二区三区久久久| 激情伊人五月天久久综合| 成人a毛片久久免费播放| 久久人人爽人人爽人人爽 | 久久精品国产亚洲av影院| 日本久久久久久中文字幕| 久久人做人爽一区二区三区 | 欧美久久久久久| 久久青青草原国产精品免费| 久久精品天天中文字幕人妻| 无码精品久久一区二区三区| 狠狠色婷婷综合天天久久丁香| 人妻无码精品久久亚瑟影视 | 中文字幕久久久久人妻| 国内精品久久久久久久久| 国产高潮久久免费观看| 久久国产精品成人免费| 国产福利电影一区二区三区,免费久久久久久久精 | 色播久久人人爽人人爽人人片AV| 欧美久久精品一级c片片| 国产精品无码久久四虎| 久久久噜噜噜久久中文字幕色伊伊| 国产精品无码久久久久| 亚洲天堂久久久| 久久99精品久久久久久| 久久无码一区二区三区少妇| 免费精品国产日韩热久久| 日韩精品久久无码人妻中文字幕| 色偷偷偷久久伊人大杳蕉| 精品国产一区二区三区久久| 久久久久亚洲AV综合波多野结衣| 亚洲а∨天堂久久精品| 四虎久久影院| 亚洲一区二区三区日本久久九|