//--《C++捷径教程》读书笔记--Chapter 11--类(第四部分)
//--Chapter 11--类
//--03/15/2006 Wed.
//--Computer Lab
//--Liwei
//--程序#4 queuq
#include <iostream>
using namespace std;
int SIZE=3;
class queue{
int sloc,rloc;
int who;
public:
int q[3];
queue(int id);
~queue();
void qput(int i);
int qget();
};
queue::queue(int id)
{
rloc=sloc=-1; q[0]=0;
who=id;
cout<<"Queue "<<who<<" initialized./n";
}
queue::~queue()
{
cout<<"Queue " <<who<<" destroyed./n";
}
void queue::qput(int i)
{ if(sloc==SIZE)
{cout<<"Queue is full./n"; return;}
sloc++;
q[sloc]=i;
}
int queue::qget()
{
if(rloc==SIZE)
{cout<<"Queue underflow./n"; return 0;}
rloc++;
return q[rloc];
}
int main()
{
queue a(1),b(2);
a.qput(10);
b.qput(19);
a.qput(20);
b.qput(1);
a.qput(999);
b.qput(999);
cout<<a.qget()<<" ";
cout<<a.qget()<<" ";
cout<<a.qget()<<"/n==========/n";
cout<<b.qget()<<" ";
cout<<b.qget()<<" ";
cout<<b.qget()<<"/n";
cout<<a.q[0]<<' '<<b.q[0]<<endl;
return 0;
}
C++队列类实现
本文介绍了一个简单的C++队列类实现,包括初始化、销毁、入队和出队等基本操作。通过示例展示了如何使用该队列类进行数据管理。

被折叠的 条评论
为什么被折叠?



