不许的近义词是什么呢:内行请进!:)
已知Q是一个非空队列,S是一个空栈.仅用栈和队列的ADT函数和一个成员变量X编写一个算法,使得Q中的元素倒置.并打印前后两次结果
运行结果:
something
gnihtemos
源码如下:
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
typedef queue<char> queue_t;
typedef stack<char> stack_t;
class TReverseQueue
{
private:
char x;
public:
void reverse_queue(queue_t &q, stack_t &s)
{
while (!q.empty())
{
x = q.front();
q.pop();
s.push(x);
cout << x;
}
cout << endl;
while (!s.empty())
{
x = s.top();
s.pop();
q.push(x);
cout << x;
}
cout << endl;
}
};
int main()
{
queue_t q;
stack_t s;
char buf[] = "something";
int len = strlen(buf);
for (int i = 0; i < len; i++)
q.push(buf[i]);
TReverseQueue rq;
rq.reverse_queue(q, s);
return 0;
}
指针加数组可以不?