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

            Tauruser

            Enjoy Every Day
            posts - 34, comments - 95, trackbacks - 0, articles - 5
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            算法與數據結構實驗(二)

            Posted on 2006-03-22 12:18 Tauruser 閱讀(717) 評論(2)  編輯 收藏 引用 所屬分類: 算法與數據結構

            數組空間組織與鏈表空間組織的堆棧實現


            為了增強實現的堆棧通用性,用堆棧實現進行模板化。代碼如下:

            //////////// //stack.h /////////////// /
            ////////////////////////////////////////////////

            #ifndef?stack_h_
            #define ?stack_h_
            #include?
            < iostream >
            using ? namespace ?std;

            template?
            < class ?T > ? class ?stack
            {
            public :
            ????
            virtual ? void ?push( const ?T? & x) = 0 ;
            ????
            virtual ? void ?pop() = 0 ;
            ????
            virtual ?T?Top()? const ? = ? 0 ;
            ????
            virtual ? bool ?IsEmpty()? const ? = 0 ;
            ????
            virtual ? bool ?IsFull()? const = 0 ;

            }
            ;

            #endif
            //////////// segstack.cpp ///////////////
            ///////// 數組組織代碼 ////////////////////////


            #include?
            " stack.h "

            template?
            < class ?T > ? class ?SegStack:? public ?stack < T >
            {
            public :
            ????SegStack(
            int ?mSize);
            ????
            ~ SegStack();
            ????
            bool ?IsEmpty()? const ;
            ????
            bool ?IsFull()? const ;
            ????
            void ?push( const ?T? & x);
            ????
            void ?pop();
            ????T?Top()?
            const ;
            ????
            // friend?ostream&?operator?<<?(ostream&?out,const?SegStack<T>&?seg);
            ????template? < ? class ?T > ?friend?ostream & ? operator ? << ?(ostream & ? out , const ?SegStack < T >& ?seg);?
            ????
            void ?output(ostream & ? out )? const ;

            private :
            ????T?
            * s;
            ????
            int ?maxSize;
            ????
            int ?top;
            }
            ;

            template?
            < class ?T > ?SegStack < T > ::SegStack( int ?mSize):top( - 1 )
            {
            ????maxSize
            = mSize;
            ????s?
            = ? new ?T[maxSize];

            }

            template?
            < class ?T > ?SegStack < T > :: ~ SegStack()
            {
            ????delete?[]s;
            }


            template?
            < class ?T > ? bool ?SegStack < T > ::IsFull()? const
            {????????
            ????
            return ?(top == (maxSize - 1 ));
            }


            template?
            < class ?T > ? bool ?SegStack < T > ::IsEmpty()? const
            {
            ????
            return ?(top ==- 1 );
            }


            template?
            < class ?T > ? void ?SegStack < T > ::push( const ?T? & x)
            {
            ????
            if (IsFull())
            ????
            {
            ????????cout
            << " The?stack?is?full " << endl;
            ????}
            else
            ????
            {
            ????????s[
            ++ top] = x;
            ????}

            }


            template?
            < class ?T > ? void ?SegStack < T > ::pop()
            {
            ????
            if (IsEmpty())
            ????
            {
            ????????cout
            << " The?stack?is?empty " << endl;
            ????}
            else ????
            ????
            {
            ????????top
            -- ;
            ????}

            }

            template?
            < class ?T > ?T?SegStack < T > ::Top()? const
            {
            ????
            return ?s[top];
            }


            template?
            < class ?T > ? void ?SegStack < T > ::output(ostream & ? out )? const
            {
            ????
            out << " The?stack?list?is: " ;
            ????
            for ( int ?i( 0 );i <= top;i ++ )
            ????????
            out << " ? " << s[i];
            ????
            // out<<endl;
            }


            template?
            < class ?T > ?ostream & ? operator ? << ?(ostream & ? out , const ?SegStack < T >& ?seg)
            {
            ????
            out << " The?stack?list?is: " ;
            ????
            for ( int ?i( 0 );i <= seg.top;i ++ )
            ????????
            out << " ? " << seg.s[i];
            ????
            // out<<endl;
            ????
            // seg.output(out);
            ???? return ? out ;
            }
            /////////////// linkstack.cpp ////////////
            //////////// //鏈表實現 ///////////////////// //

            #include? " stack.h "

            template?
            < class ?T1 > ? struct ?Element
            {
            ????T1?content;
            ????Element
            * ?next;
            }
            ;
            template?
            < class ?T1 > ? class ?LinkStack:? public ?stack < T1 >
            {
            public :
            ????LinkStack();
            ????
            ~ LinkStack();
            ????
            bool ?IsEmpty()? const ;
            ????
            bool ?IsFull()? const ;
            ????
            void ?push( const ?T1? & x);
            ????
            void ?pop();
            ????T1?Top()?
            const ;
            ????template?
            < class ?T > ?friend?ostream & ? operator << (ostream & ? out ,? const ?LinkStack < T1 >& ?linkstack);
            ????
            void ?output(ostream & ? out )? const ;

            private :

            ????Element
            < T1 >* ?top;
            }
            ;
            template?
            < class ?T1 > ? bool ?LinkStack < T1 > ::IsEmpty()? const
            {
            ????
            if (top == NULL)
            ????????
            return ? true ;
            ????
            else
            ????????
            return ? false ;
            }

            template?
            < class ?T1 > ? bool ?LinkStack < T1 > ::IsFull()? const
            {
            ????
            return ? false ;
            }

            template?
            < class ?T1 > ?LinkStack < T1 > ::LinkStack():top(NULL)
            {
            }

            template?
            < class ?T1 > ?LinkStack < T1 > :: ~ LinkStack()
            {
            ????
            while (top != NULL)
            ????
            {
            ????????Element
            < T1 >* ?temp;
            ????????temp
            = top;
            ????????top
            = top -> next;
            ????????delete?temp;
            ????}

            }


            template?
            < class ?T1 > ? void ?LinkStack < T1 > ::push( const ?T1? & x)
            {
            ????Element
            < T1 >* ?temp = new ?Element < T1 > ;
            ????temp
            -> content = x;
            ????temp
            -> next = top;
            ????top
            = temp;
            }

            template?
            < class ?T1 > ? void ?LinkStack < T1 > ::pop()
            {
            ????
            if (top != NULL)
            ????
            {
            ????????Element
            < T1 >* ?temp;
            ????????temp
            = top;
            ????????top
            = top -> next;
            ????????delete?temp;
            ????}

            ????
            }


            template?
            < class ?T1 > ?T1?LinkStack < T1 > ::Top()? const
            {
            ????
            return ?top -> content;
            ????
            }

            template?
            < class ?T1 > ?ostream & ? operator << (ostream & ? out ,? const ?LinkStack < T1 >& ?linkstack)
            {
            ????Element
            < T1 >* ?temp;
            ????temp
            = linkstack.top;

            ????
            out << " The?stack?list?is: " ;
            ????
            while (temp != NULL)
            ????
            {
            ????????
            out << temp -> content << ' ? ' ;
            ????????temp
            = temp -> next;
            ????}


            ????
            return ? out ;
            }

            template?
            < class ?T1 > ? void ?LinkStack < T1 > ::output(ostream & ? out )? const
            {
            ????Element
            < T1 >* ?temp;
            ????temp
            = top;

            ????
            out << " The?stack?list?is: " ;
            ????
            while (temp != NULL)
            ????
            {
            ????????
            out << temp -> content << ' ? ' ;
            ????????temp
            = temp -> next;
            ????}

            }

            沒有寫注釋,有空再補上吧。

            Feedback

            # re: 算法與數據結構實驗(二)  回復  更多評論   

            2006-03-22 17:33 by 任我行
            template < class T > class stack
            {
            public :
            virtual void push( const T & x) = 0 ;
            virtual void pop() = 0 ; // 這樣子void 類型有些不妥吧。

            # re: 算法與數據結構實驗(二)  回復  更多評論   

            2006-03-22 18:15 by Tauruser
            嗯,你認為應該如何呢?
            我知道有些stack結構直接用pop(),彈出并返回棧頂,但這個模板類已經有一個Top()函數可以做到這個,將pop()返回值設為void有什么不妥呢?
            久久丫精品国产亚洲av| 亚洲国产精品成人久久| 国产精品无码久久久久| 99久久中文字幕| 久久激情亚洲精品无码?V| 久久亚洲AV无码精品色午夜| 国产综合久久久久| 亚洲国产成人精品女人久久久| 青青草原精品99久久精品66| 狠狠色综合久久久久尤物| 国产成人精品三上悠亚久久| 精品一区二区久久| 国产综合免费精品久久久| 久久人人爽人人爽人人AV东京热| 久久精品国产亚洲7777| 久久亚洲美女精品国产精品| 久久亚洲精品无码播放| 日本免费久久久久久久网站| 久久精品国产久精国产果冻传媒| 国产99久久久国产精品~~牛| 国产精品美女久久福利网站| 久久精品18| 精品免费久久久久国产一区| 精品久久久久久国产91| 亚洲AV无码久久| 中文字幕日本人妻久久久免费| 亚洲午夜福利精品久久| 青青久久精品国产免费看| 国产成人无码精品久久久免费| 国产精品视频久久| 国产精品久久一区二区三区| 国产精品禁18久久久夂久| 亚洲乱码中文字幕久久孕妇黑人| 性做久久久久久久久| 久久亚洲av无码精品浪潮| 久久99精品免费一区二区| 国产精品美女久久久久av爽| 麻豆精品久久久一区二区| 一本伊大人香蕉久久网手机| 久久国产精品99久久久久久老狼| 久久精品国产秦先生|