【问题标题】:reverse of a queue in c++C ++中队列的反转
【发布时间】:2015-01-08 15:23:51
【问题描述】:

这是我被困在下面的试卷中的一个问题,尽管我无法完成,但我已经附上了这个问题,但我已经完成了一些。

问题:

使用堆栈和队列类的以下类定义编写一个模板函数reverseQueue(?),它将指向队列的指针作为参数并使用堆栈对象(或指向堆栈对象的指针)来反转给定的队列。对 reverseQueue 的函数调用应该反转作为参数传递的队列的数据。 [提示:只需在你的 reverseQueue(?) 函数中调用下面给出的适当方法。您不必为下面给出的堆栈和队列类/方法编写实现代码。只需阅读每种方法的作用并根据需要使用它们。]

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
};

我的回答不完整,因为我无法继续下面是我的回答,无论我做了什么

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
} 

如果有人能给出答案,那就太好了

【问题讨论】:

  • 看起来stackqueue基本一样,只是顺序颠倒了。所以你已经开始了;将队列中的项目(例如1, 2, 3)移动到堆栈将反转它们的顺序(例如3, 2, 1)。现在您需要将它们从堆栈中弹出并重新添加到队列中。
  • @JonathanPotter 但这正是我想不出我能写的代码的地方,我知道这个概念,但不知道实现它的代码
  • 不要看私处。你不需要一个指针。
  • @molbdnilo 我现在所做的一切对吗?
  • 基本上你想要while (stack.pop) queue.enqueue()

标签: c++ stack queue


【解决方案1】:

假设输入队列看起来像

 1    2    3    4
 ^              ^
front           back

如果我们从中取出项目,我们将得到 1、2、3、4。

现在假设我们在将这些项目出列时将它们压入堆栈。
它看起来像这样:

4  <- top
3
2
1  <- bottom

如果我们弹出这些,我们将得到 4、3、2、1。

现在,如果我们在将它们从堆栈中弹出时将它们排入队列,我们​​将得到

 4    3    2    1
 ^              ^
front           back

与原始队列相反。

应该这样做:

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);
    }
} 

【讨论】:

    【解决方案2】:
    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;
    } 
    
    • 运行时复杂度:O(n) + O(n) = O(2*n) ~ O(n)
    • 空间复杂度:stack_ptr 的 O(n)

    【讨论】:

    • 空间复杂度实际上是恒定的。
    • 是的,如果我们以不同的方式实现它,它可以保持不变,但在我的解决方案中,它仍然是 O(n),因为我在第一个 while 循环中将所有内容都放入堆栈中。
    • @glaze 您正在从队列中删除每个项目,因此使用的空间是恒定的。
    • 写 while(true){if(c){s;}else{break;}) 而不是 while(c)s;不是很好的风格。它接近于混淆。
    猜你喜欢
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多