• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            數(shù)組空間組織與鏈表空間組織的堆棧實(shí)現(xiàn)


            為了增強(qiáng)實(shí)現(xiàn)的堆棧通用性,用堆棧實(shí)現(xiàn)進(jìn)行模板化。代碼如下:

            //////////// //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 ///////////////
            ///////// 數(shù)組組織代碼 ////////////////////////


            #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 ////////////
            //////////// //鏈表實(shí)現(xiàn) ///////////////////// //

            #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;
            ????}

            }

            沒有寫注釋,有空再補(bǔ)上吧。

            Feedback

            # re: 算法與數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)(二)  回復(fù)  更多評(píng)論   

            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: 算法與數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)(二)  回復(fù)  更多評(píng)論   

            2006-03-22 18:15 by Tauruser
            嗯,你認(rèn)為應(yīng)該如何呢?
            我知道有些stack結(jié)構(gòu)直接用pop(),彈出并返回棧頂,但這個(gè)模板類已經(jīng)有一個(gè)Top()函數(shù)可以做到這個(gè),將pop()返回值設(shè)為void有什么不妥呢?
            俺来也俺去啦久久综合网| 久久精品国产清自在天天线| 精品久久久久久中文字幕人妻最新| 久久国产亚洲精品无码| 久久精品国产第一区二区| 99久久综合国产精品免费| 成人资源影音先锋久久资源网| 99久久99久久精品国产片| 中文字幕人妻色偷偷久久| 99国内精品久久久久久久| 少妇内射兰兰久久| 青青青青久久精品国产h久久精品五福影院1421 | 精品综合久久久久久88小说| 久久人妻AV中文字幕| 国产三级精品久久| 精品国产乱码久久久久久郑州公司| 久久男人中文字幕资源站| 久久人人爽人人爽人人片av高请| 久久久精品日本一区二区三区 | 亚洲狠狠综合久久| 青草国产精品久久久久久| 女人高潮久久久叫人喷水| 国产综合精品久久亚洲| 99久久婷婷国产综合亚洲| 日韩乱码人妻无码中文字幕久久| 99久久香蕉国产线看观香| 久久精品中文字幕有码| 99久久精品国产一区二区三区| 精品久久久久香蕉网| 久久久久久国产精品无码超碰| 人妻无码αv中文字幕久久琪琪布| 中文字幕精品久久| 无码国内精品久久综合88| 亚洲伊人久久成综合人影院| 久久免费视频6| 日本精品久久久久久久久免费| 久久成人精品| 色悠久久久久久久综合网| 亚洲v国产v天堂a无码久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 国产精品9999久久久久|