• <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 閱讀(713) 評論(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无码专区| 久久久久九国产精品| 亚洲午夜久久久久妓女影院| 国产精品美女久久久网AV| 久久久久亚洲AV无码永不| 亚洲国产天堂久久综合| 国产精品一久久香蕉国产线看| 伊人热热久久原色播放www| 国产成人无码精品久久久免费| 伊人久久大香线蕉综合影院首页| 99久久无码一区人妻| 久久棈精品久久久久久噜噜| 久久久久久久免费视频| 久久久91人妻无码精品蜜桃HD| 久久99国产精品一区二区| 日产精品久久久久久久| 久久亚洲日韩看片无码| 亚洲国产日韩欧美久久| 婷婷综合久久中文字幕| 久久精品国产影库免费看| 久久精品无码一区二区无码 | 99久久99久久精品国产片果冻| 日产精品久久久一区二区| 久久久久久久久久久久久久 | 狠狠88综合久久久久综合网 | 久久青青草原精品影院| 久久久婷婷五月亚洲97号色| 综合久久精品色| 亚洲精品国产自在久久| 欧洲性大片xxxxx久久久| 久久久精品视频免费观看| 亚洲国产日韩欧美久久| 久久伊人精品一区二区三区| 久久精品一本到99热免费|