[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;
class queue {
int q[100];
int sloc, rloc;
public:
void init();
void qput(int i);
int qget();
};
void queue::init()
{
rloc = sloc = 0;
}
void queue::qput(int i)
{
if (sloc == 100) {
cout << "Queue is full.\n";
return ;
}
sloc++;
q[sloc] = i;
}
int queue::qget()
{
if (rloc == sloc) {
cout << "Queue underflow.\n";
return 0;
}
rloc++;
return q[sloc];
}
int main(void)
{
queue a, b;
a.init();
b.init();
cout << a.sloc << " ";
cout << b.sloc << "\n";
return 0;
}
[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;
class queue {
int q[100];
int sloc, rloc;
public:
int anybody;
void init();
void qput(int i);
int qget();
};
//初始化
void queue::init()
{
rloc = sloc = 0;
}
//将一个整数放到队列中
void queue::qput(int i)
{
if (sloc == 100) {
cout << "Queue is full.\n";
return ;
}
sloc++;
q[sloc] = i;
}
//从队列中获得一个整数
int queue::qget()
{
if (rloc == sloc) {
cout << "Queue underflow.\n";
return 0;
}
rloc++;
return q[sloc];
}
int main(void)
{
queue a, b;
a.init();
b.init();
a.qput(10);
b.qput(30);
a.qput(20);
b.qput(40);
cout << a.qget() << " ";
cout << a.qget() << "\n";
cout << b.qget() << " ";
cout << b.qget() << "\n";
a.anybody = 99;
b.anybody = 100;
cout << "a.anybody: " << a.anybody << "\n";
cout << "b.anybody: " << b.anybody << "\n";
return 0;
}