2.用两个顺序存储结构的栈实现一个队列

本文介绍了一种使用两个栈来实现队列的方法。通过栈的压入和弹出操作,实现了队列的基本功能,包括入队、出队、判断队列是否为空等。示例代码展示了如何利用两个栈进行数据的临时转移以实现先进先出的原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       设计一个队列,要求用两个顺序存储结构的栈,通过这两个栈的相关运算实现队列的以下功能:

public:
Queue();//构造函数
bool IsEmpty(); //返回队列是否已空
bool IsFull();//返回队列是否已满
int Serve(string& item);//出队列,赋值于item,成功返回0,否则返回-1
int Append(const string &item); //item进队列,成功返回0,否则返回-1
int Retrieve(string& item) const; //将队列头的值赋给item,成功返回0,否则返回-1

代码如下:

#include<iostream>
#include<string>
using namespace std;
const int maxstack = 10; 
class Stack
{
public:
   Stack();
   bool empty() const;
   string pop();
   string gettop();
   void push(const string &item);
private:
 friend class Queue;
 int count;
    string entry[maxstack];
};
void Stack::push(const string &item)
{
 if(count>=maxstack)
  cout<<"overflow "<<endl;
 else
  entry[count++]=item;
}
string Stack::pop()
{
 string item;
  if(count==0)
  { 
   item="underflow";
      return item;
  }
  else
  {
   item=entry[count-1];
      --count;
      return item;
  }
}
string Stack::gettop()
{
 string item;
  if(count==0)
  {
   item="underflow";
  
  }
  else
  {
   item=entry[count-1];     
  }
  return item;
 }
bool Stack::empty() const
{
  bool outcome=true;
  if(count>0)
  outcome=false;
  return outcome;
}
Stack::Stack()
{
count=0;
}
class Queue
{
   public:
      Queue();//构造函数
      bool IsEmpty(); //返回队列是否已空
      bool IsFull();//返回队列是否已满
      int Serve(string& item);//出队列,赋值于item,成功返回0,否则返回-1
      int Append(const string &item); //item进队列,成功返回0,否则返回-1
      int Retrieve(string &item) const; //将队列头的值赋给item,成功返回0,否则返回-1
      int Print();

 private:
    mutable Stack a;
    mutable Stack b;
};
Queue::Queue()
{

}
bool Queue::IsEmpty()
{
    if((a.empty()==true)&&(b.empty()==true))
        return true;
 else
  return false;
}
bool Queue::IsFull()
{  
 if(a.count>=maxstack)
  {
    return true;
  }
 else
  return false;
}
int Queue::Append(const string &item)
{
 if(IsFull()==true)
  return -1;
     a.push(item);
        return 0;
}
int Queue::Serve(string &item)

 
   if(a.empty() == true)
            {
                item ="/0";
                return -1;
            }
            else
            {
                while (a.empty() == false)
                {
                    b.push(a.pop());
                }

                item = b.pop();

                while (b.empty() == false)
                {
                    a.push(b.pop());
                }

                return 0;
            }
}
int Queue::Retrieve(string &item) const
{
          if (a.empty() == true)
            {
                item ="/0";
                return -1;
            }
            else
            {
                while (a.empty() == false)
                {
                    b.push(a.pop());
                }

                item = b.gettop();

                while (b.empty() == false)
                {
                   a.push(b.pop());
                }

                return 0;
            }           
}
int Queue::Print()
{
 
           if (a.empty() == true)
            {
                return -1;
            }
            else
            {
                cout<<"当前队列元素为: ";

                while (a.empty() == false)
                {
                    b.push(a.pop());
                }               

                while (b.empty() == false)
                {                 
                    cout<<b.gettop()<<" ";
                    a.push(b.pop());
                }
               
                cout<<endl;

                return 0;
            }

}

int main()
{
   Queue q;
   cout<<"1)入队列:Jan,Feb,Mar,Apr,May"<<endl;

   q.Append("Jan");
   q.Append("Feb");
   q.Append("Mar");
   q.Append("Apr");
   q.Append("May");
   q.Print();
   cout<<endl;

   cout<<"2) 出队列二次 - 即Jan、Feb出队列"<<endl;         
   string temp;
   q.Serve(temp);
   q.Serve(temp);
   q.Print();
   cout<<endl;

   cout<<"3) 入队列Jun、Ocb"<<endl;           
            q.Append("Jun");
            q.Append("Oct");
            q.Print();
   cout<<endl;
  
   cout<<"4) 出队列二次"<<endl;
   string temp2;
   q.Serve(temp2);
   q.Serve(temp);
   q.Print();
   cout<<endl;
  
   cout<<"5) Dec入队列"<<endl;
   q.Append("Dec");
   q.Print();

  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值