2.用两个顺序存储结构的栈实现一个队列
2010-07-15 15:03
513 查看
设计一个队列,要求用两个顺序存储结构的栈,通过这两个栈的相关运算实现队列的以下功能:
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;
}
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;
}
相关文章推荐
- 面试题--用两个栈实现一个队列
- Python剑指offer之两个栈实现一个队列-两个队列实现一个栈
- 面试题7:用两个栈实现队列和用两个队列实现一个栈
- 两个栈实现一个队列
- 数据结构与算法之—两个栈实现一个队列
- 用两个栈实现一个队列
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 两个栈实现一个队列
- 栈&队列面试题之两个队列实现一个栈
- 《剑指Offer》用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 剑指offer 7---使用两个队列实现一个栈
- 用两个栈实现一个队列的功能
- 多态&指针访问虚函数&不能被继承的类&快速排序&N皇后问题&插入排序&堆排序&merge归并排序&栈上生成对象&两个栈实现一个队列
- C++两个队列实现一个栈
- 两个栈实现一个队列的思路
- 两个栈来实现一个队列以及两个队列实现一个栈
- 两个栈实现一个队列
- Java用两个队列实现一个栈的功能
- (LeetCode)两个队列来实现一个栈