C++ c+中队列的反转+;
这是一道题,我被卡在下面的试卷上。我附上了这道题,虽然我没能完成,但我已经做了一些 问题: 使用以下堆栈和队列类的类定义编写一个模板化函数reverseQueue(?),该函数将指向队列的指针作为参数,并使用堆栈对象(或指向堆栈对象的指针)来反转给定的队列。对reverseQueue的函数调用应反转作为参数传递的队列数据。[提示:只需在reverseQueue(?)函数中调用下面给出的适当方法。您不必为下面给出的堆栈和队列类/方法编写实现代码。只需阅读每个方法的功能,并根据需要使用它们。]C++ c+中队列的反转+;,c++,stack,queue,C++,Stack,Queue,这是一道题,我被卡在下面的试卷上。我附上了这道题,虽然我没能完成,但我已经做了一些 问题: 使用以下堆栈和队列类的类定义编写一个模板化函数reverseQueue(?),该函数将指向队列的指针作为参数,并使用堆栈对象(或指向堆栈对象的指针)来反转给定的队列。对reverseQueue的函数调用应反转作为参数传递的队列数据。[提示:只需在reverseQueue(?)函数中调用下面给出的适当方法。您不必为下面给出的堆栈和队列类/方法编写实现代码。只需阅读每个方法的功能,并根据需要使用它们。] te
template <class T>
struct NODE {
NODE<T> *pNext;
T Data;
};
template <class T>
class stack{
private:
NODE<T> * top;
public:
stack();
~stack();
void push (T data); //pushes a new node with data type //T in a stack
bool pop (T &data); //pops out the top most node from //the stack
void printStack(); //prints all elements of a stack
};
template <class T>
class queue {
private:
NODE<T> * front;
NODE<T> * tail;
public:
queue ();
~queue();
bool dequeue ( T & data); //removes first node from //a queue
bool enqueue (T val); //appends a new node in a //queue
};
模板
结构节点{
节点*pNext;
T数据;
};
模板
类堆栈{
私人:
节点*顶部;
公众:
堆栈();
~stack();
void push(T data);//在堆栈中推送数据类型为//T的新节点
bool-pop(T&data);//从堆栈中弹出最顶部的节点//
void printStack();//打印堆栈的所有元素
};
模板
类队列{
私人:
节点*前端;
节点*尾部;
公众:
队列();
~queue();
bool dequeue(T&data);//从//队列中删除第一个节点
bool enqueue(T val);//在//队列中追加一个新节点
};
我的答案是不完整的,因为我无法继续下去。下面是我的答案,无论我做了什么
template <class T>
void reverseQueue(queue <T> *ptr){
stack <T> *stackptr= new stack<T>;
T temp;
while(ptr->front !=NULL){
temp=ptr->Data;
ptr->dequee(ptr->Data);
stackptr->push(temp);
}
// incomplete code
}
模板
无效反向队列(队列*ptr){
stack*stackptr=新堆栈;
温度;
同时(ptr->front!=空){
温度=ptr->数据;
ptr->DEQUE(ptr->Data);
堆垛机->推送(温度);
}
//不完全代码
}
如果有人能给出答案,那就太好了假设输入队列如下
1 2 3 4
^ ^
front back
如果我们将项目从队列中取出,我们将得到1、2、3、4
现在假设我们在将这些项目出列时将它们推到堆栈上。它看起来是这样的:
4 <- top
3
2
1 <- bottom
这与原始队列相反
像这样的东西应该可以做到:
template <class T>
void reverseQueue(queue <T> *q){
stack <T> s;
T temp;
// First build a stack (LIFO queue) from the (FIFO) queue.
while (q->dequeue(temp))
{
s.push(temp);
}
// The first item in the queue is now at the bottom of the stack.
// The last item is at the top.
// The queue is empty.
// If we enqueue them again they will be reversed.
while (s.pop(temp))
{
q->enqueue(temp);
}
}
模板
无效反向队列(队列*q){
堆栈s;
温度;
//首先从(FIFO)队列构建堆栈(后进先出队列)。
while(q->dequeue(temp))
{
s、 推动(温度);
}
//队列中的第一项现在位于堆栈的底部。
//最后一项在顶部。
//队列是空的。
//如果我们再让他们排队,他们会被倒过来。
while(s.pop(temp))
{
q->排队(临时);
}
}
模板
无效反向队列(队列*q_ptr)
{
堆栈*stack_ptr=新堆栈();
温度;
while(true)
{
if(q_ptr.出列(临时))
{
堆栈ptr->推送(温度);
}
其他的
{
打破
}
}
while(true)
{
如果(堆栈ptr->pop(临时))
{
q_ptr.推力(温度);
}
其他的
{
打破
}
}
删除stack_ptr;
}
- 运行时复杂性:O(n)+O(n)=O(2*n)~O(n)
- 空间复杂度:堆栈的O(n)_ptr
堆栈
与队列
基本相同,只是顺序相反。所以你开始得很好;将项目从队列(例如1,2,3
)移动到堆栈将颠倒其顺序(例如3,2,1
)。现在你需要将它们从堆栈中弹出并添加到队列中。@JonathanPotter但那正是我想不出我能写的代码的地方我知道这个概念但不知道要写的代码不要看私有部分。你不需要一个指针。@molbdnio无论我现在做了什么,它都是正确的吗?基本上你想要while(stack.pop)queue.enqueue()
。空间复杂性实际上是常数。是的,如果我们以不同的方式实现它,它可以是常数,但在我的解决方案中它仍然是O(n)由于我将所有内容都放在第一个while循环的堆栈中,所以要从队列中删除每个项,所以使用的空间是恒定的。用while(true){if(c){s;}else{break;}代替while(c)s;,写while(true){if(c){break;}不是一种好的样式。它近乎于混淆。
template <class T>
void reverseQueue(queue <T> *q){
stack <T> s;
T temp;
// First build a stack (LIFO queue) from the (FIFO) queue.
while (q->dequeue(temp))
{
s.push(temp);
}
// The first item in the queue is now at the bottom of the stack.
// The last item is at the top.
// The queue is empty.
// If we enqueue them again they will be reversed.
while (s.pop(temp))
{
q->enqueue(temp);
}
}
template <class T>
void reverseQueue(queue<T> *q_ptr)
{
stack<T> *stack_ptr = new stack<T>();
T temp;
while(true)
{
if (q_ptr.dequeue(temp))
{
stack_ptr->push(temp);
}
else
{
break;
}
}
while(true)
{
if (stack_ptr->pop(temp))
{
q_ptr.push(temp);
}
else
{
break;
}
}
delete stack_ptr;
}