• <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年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(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 楊彬彬 閱讀(1752) 評論(0)  編輯 收藏 引用 所屬分類: 數據結構
            久久婷婷成人综合色综合| 亚洲香蕉网久久综合影视 | 久久er热视频在这里精品| 伊人色综合久久| 2021国产精品午夜久久| 国产精品久久久久影视不卡| 精品久久久久久久久免费影院| 欧美激情一区二区久久久| 狠狠色婷婷久久一区二区三区| 91麻豆国产精品91久久久| 精品无码久久久久国产| 久久综合色之久久综合| 久久91综合国产91久久精品| 久久青青草原精品国产软件| 久久久精品国产sm调教网站 | 亚洲狠狠婷婷综合久久蜜芽| 色播久久人人爽人人爽人人片aV| 久久亚洲精品成人av无码网站| 精品国产综合区久久久久久| 成人精品一区二区久久| 狠狠色丁香久久综合婷婷| 午夜精品久久久久久影视riav| 久久av免费天堂小草播放| 青青草原精品99久久精品66| 久久久WWW成人免费精品| 国产三级精品久久| 99久久免费国产特黄| 久久久久亚洲AV无码网站| 国产精品99久久久久久宅男小说| 久久国产乱子伦精品免费午夜| 丰满少妇人妻久久久久久| 日韩精品久久无码人妻中文字幕| 久久99九九国产免费看小说| 欧美日韩成人精品久久久免费看| 狠狠久久综合| 亚洲国产成人久久精品99| 国产69精品久久久久观看软件| 欧美久久久久久午夜精品| 色综合合久久天天给综看| 久久青青草原精品国产不卡| 久久91精品国产91|