您的位置:首页 > 其它

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: