【问题标题】:Remove all item from queue c++从队列c ++中删除所有项目
【发布时间】:2012-10-26 06:12:39
【问题描述】:

如果我将队列实现为一系列节点(值,指向下一个节点的指针),那么横穿该队列并检查特定值并编辑队列以使所有节点包含该值的最佳方法是什么值将被删除。但是队列的顺序会保持不变。

好的,这里是描述所有功能的标题

class queue
{
  public:
    queue(); // constructor - constructs a new empty queue.
    void enqueue( int item ); // enqueues item.
    int dequeue();  // dequeues the front item.
    int front();   // returns the front item without dequeuing it.
    bool empty();  // true iff the queue contains no items.
    int size();  // the current number of items in the queue.
    int remove(int item); // removes all occurrances of item 
      // from the queue, returning the number removed.

  private:
    class node  // node type for the linked list 
    {
       public:
           node(int new_data, node * next_node ){
              data = new_data ;
              next = next_node ;
           }
           int data ;
           node * next ;
    };

    node* front_p ;
    node* back_p ;
    int current_size ; // current number of elements in the queue.
};

这是 queue.cpp

#include "queue.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

queue::queue()
{
   front_p = NULL;
   back_p = NULL;
   current_size = 0;
}

void queue::enqueue(int item)
{
    node* newnode = new node(item, NULL);
   if (front_p == NULL) //queue is empty
        front_p = newnode;
    else
        back_p->next = newnode;
   back_p = newnode;
   current_size ++;
}

int queue::dequeue()
{
   //if there is only one node
    int value = front_p->data;
    if (front_p == back_p)
    {
        front_p = NULL;
        back_p = NULL;
    }
    //if there are two or more
    else
    {
        node* temp = front_p;
        front_p = temp->next;
        delete temp;
    }
    current_size --;
    return value;
}

int queue::front()
{
    if (front_p != NULL)
        return front_p->data;
}

bool queue::empty()
{
    if (front_p == NULL && back_p == NULL)
        return true;
    else
        return false;
}

int queue::size()
{
    return current_size;
}

int queue::remove(int item)
{
//?????
}

【问题讨论】:

  • 检查特定值是什么意思?检查队列中是否存在特定值?
  • 好吧,您只需遍历它并删除节点即可。如果没有看到您的实施,我真的不明白我们如何可以在这里为您提供帮助。
  • 最好的方法是编写一些完全按照您所说的代码。对不起,如果这看起来很滑稽,但你期待什么样的答案?除非您对问题所在有所了解(例如通过发布代码或解释您不理解的问题),否则很难提供帮助。我猜你对如何从列表中删除节点感到困惑,但如果是这种情况,请说出来。
  • 如果需要修改/删除中间的项目,队列不是一个好的数据结构。你在使用例如std::queue 或您自己的本土队列?如果是后者,你使用的是std::list吗?
  • 如果您正在使用例如std::list 用于队列,或另一个暴露标准迭代器接口的容器,请考虑 std::remove_if

标签: c++ linked-list queue nodes


【解决方案1】:

您需要遍历列表,检查每个节点的值。如果您看到一个序列 A -> B -> C,其中 B 是您要删除的值,您需要将链接从 A 更改为指向 C 而不是 B。

为方便起见,您需要保留对您看到的最后一个节点以及当前节点的引用。如果当前节点的值等于要删除的值,则将最后一个节点的 next 指针更改为当前节点的 next 指针。确保在继续之前释放当前节点的内存。

【讨论】:

    【解决方案2】:

    如果您的队列公开标准迭代器,最好的方法是使用标准算法:

    queue.erase(std::remove(queue.begin(), queue.end(), value_to_remove), queue.end());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 2012-07-12
      • 2011-09-24
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多