亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 2077 | 回復(fù): 3
打印 上一主題 下一主題

[C++] 兩個(gè)棧模仿隊(duì)列,麻煩老師指出錯(cuò)誤! [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2014-04-15 20:08 |只看該作者 |倒序?yàn)g覽
本帖最后由 nOneee_ 于 2014-04-15 21:55 編輯

#include<iostream>
//#include"8.h"
using namespace std;



template<class T>
class Stack
{
        public:
                void clear();
                bool push(T item);
                bool pop(T & item);
                bool isEmpty();        
                bool isFull();
};

template<class T>
class arrStack:public Stack<T>
{
        public:
                int mSize;
                int top;

        public:
                T   *st;
                arrStack(int size)
                {
                        mSize=size;
                        top=-1;
                        st=new T[mSize];
                }
        arrStack()
                {
                        top=-1;
                }
                ~arrStack()
                {
                        delete[]st;
                }
                void clear()
                {
                        top=-1;
                }
                bool push(T item)
                {
                        if(top==mSize-1)
                        {
                                cout<<"棧滿溢出"<<endl;
                                return false;
                        }
                        else
                        {
                                st[++top]=item;
                                return true;
                        }
                }
                bool pop(T & item)
                {
                        if(top==-1)
                        {
                                cout<<"棧為空,不能執(zhí)行出棧操作"<<endl;
                                return false;
                        }
                        else
                        {
                                item=st[top--];
                                cout<<item<<" ";
                                return true;
                    
                        }
                }
                bool isEmpty()
                {
                        if(top==-1)
                        {
                                cout<<"?"<<endl;
                        return 1;
                        }
                else
                        {
                        cout<<"棧非空"<<endl;
                        return 0;
                        }
                }
                bool isFull()
                {
                        if(top==mSize-1)
                        {
                        cout<<"棧滿"<<endl;
                        return 0;
                        }
                else
                        {
                        cout<<"棧未滿"<<endl;
                        return 1;
                        }        
                }

        };

template<class T>
class CopyQueue
{
        private:
                arrStack<T> s1;
                arrStack<T> s2;
    public:
                        
        int enqueue(T x)                      // 用入棧模擬入隊(duì)
                {

                        if(s1.isFull() && !(s2.isEmpty()))
                        {
                                cout<<"隊(duì)列已滿"<<endl;
                                return false;
                        }                                   //s1滿s2非空,這時(shí)s1不能再入棧
            if(s1.isFull() && (s2.isEmpty()))   //s2空,將s1退棧, 再壓棧到s2
                        {
                                while(!(s1.isEmpty()))
                                {
                                          
                                    s2.push(s1.pop(x));
                                        cout<<"s1棧中的數(shù)字移至s2棧"<<endl;
                                }
                        }
                        s1.push(x);


            return 1;                          //x入棧,實(shí)現(xiàn)了隊(duì)列元素的入隊(duì)
                }

                void dequeue()                         //s2是輸出棧,將s2棧頂元素退棧,實(shí)現(xiàn)隊(duì)列元素的出隊(duì)
                {
                        int x;
                        if(!(s2.isEmpty()))                //棧s2不空,則直接出隊(duì)
                        {
                                s2.pop(x);     
                                cout<<"出隊(duì)對(duì)列為:"<<x<<" ";  
                        }
            else if((s1.isEmpty()))                           //處理s2空棧   
            {
                                cout<<"輸入完畢,s1?"<<endl;                //若輸入棧也空,則隊(duì)空
                //                return false;
                        }
            else
            {
                                while(!(s1.isEmpty()))                  //先將棧s1倒入s2中,再出隊(duì)
                {
                                
                                        s2.push(s1.pop(x));
                    s2.pop(x);                          //s2退棧相當(dāng)隊(duì)列出隊(duì)
                    cout<<"輸出對(duì)列為:"<<x<<endl;
                                }
                        }                                           //結(jié)束算法dequue
                }
                int queue_empty()                               //本算法判用棧s1和s2模擬的隊(duì)列是否為空
                {
                        if((s2.isEmpty()) && (s1.isEmpty()))
                        {
                                cout<<"s1,s2棧均為空"<<endl;
                                return 1;                       //隊(duì)列空
                        }
                                               
            else
                            return 0;                       //隊(duì)列不空
                }
};


               
void main()
{
    int n;
        CopyQueue<int>x;



        x.queue_empty();

        
        cout<<"入棧順序?yàn)?限4個(gè)數(shù)):"<<" ";
        for(int i=0;i<=3;i++)
        {
                cin>>n;
        x.enqueue(n);
               
        }

    cout<<"出棧順序?yàn)?";
    for(int j=0;j<=3;j++)
        {
                x.dequeue();
        }  
}   

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2014-04-15 21:24 |只看該作者
為什么不用代碼編輯模式???

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2014-04-15 21:50 |只看該作者
回復(fù) 2# libo26_lee
請(qǐng)問(wèn)如何修改?


   

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
4 [報(bào)告]
發(fā)表于 2014-04-16 18:55 來(lái)自手機(jī) |只看該作者
無(wú)參的構(gòu)造和析構(gòu)不嚴(yán)謹(jǐn)?罩羔樤L問(wèn)會(huì)拋出異常。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP