• <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有什么不妥呢?
            青青国产成人久久91网| 久久午夜免费视频| 久久久久亚洲AV无码永不| 午夜精品久久久久久久久| 97久久天天综合色天天综合色hd| 色综合合久久天天综合绕视看| 国产精品免费久久久久电影网| 久久涩综合| 国产精品99久久久久久人| 人妻少妇精品久久| 狠狠狠色丁香婷婷综合久久俺| 久久综合亚洲色HEZYO国产| 无码人妻久久一区二区三区| 久久精品国产免费| 少妇久久久久久久久久| 青青国产成人久久91网| 亚洲精品国产字幕久久不卡| 国产伊人久久| 久久国产精品久久精品国产| 亚洲精品午夜国产va久久 | 国产精自产拍久久久久久蜜| 国产成年无码久久久免费| 国产日韩欧美久久| 国产精品久久成人影院| 精品伊人久久大线蕉色首页| 久久综合日本熟妇| 国产精品综合久久第一页| 久久国产精品久久| 波多野结衣中文字幕久久| 77777亚洲午夜久久多喷| 要久久爱在线免费观看| 香蕉aa三级久久毛片| 国产日韩久久久精品影院首页| 国产999精品久久久久久| 久久青青草原国产精品免费 | 久久久久香蕉视频| 亚洲乱亚洲乱淫久久| 亚洲国产成人久久精品影视| 热久久这里只有精品| 狠狠色丁香婷婷久久综合不卡| 久久久久人妻一区精品色|