【问题标题】:Linked List Enqueue and Dequeue链表入队和出队
【发布时间】:2015-10-26 03:05:22
【问题描述】:

对于使用 c++ 实现的链表实现的队列,我的入队和出队遇到了一些问题。我的老师说模板是禁止使用的,我不能更改公共和私人功能,因为他给了我们它们。我不断收到分段错误。我真的不明白我做错了什么。我还包含了标头以及入队和出队函数。

标题

const int MAX_STRING = 6;

typedef char Element300[MAX_STRING + 1];

class Queue300
{

    public:
        Queue300();
        Queue300(Queue300&);
        ~Queue300();
        void enQueue300(const Element300);
        void deQueue300(Element300);
        void view300();

    private:
        struct Node300;
        typedef Node300 * NodePtr300;
        struct Node300
        {
            Element300 element;
            NodePtr300 next;
        };
        NodePtr300 front, rear;
};

入队

void Queue300::enQueue300(const Element300 input)


{
    NodePtr300 temp = NULL;

    temp = new (std::nothrow) Node300;

    if (temp == NULL)
    {
        cerr << "The queue is full, could not add(enqueue) any more elements." << endl;
    }

    else if (front == NULL && rear == NULL)
    {
        strcpy(temp->element, input);
        rear = temp;
        rear->next = NULL;
        front = rear;
        temp = NULL;
    }

    else
    {
        strcpy(temp->element, input);
        temp = rear->next;
        rear = temp;
        rear->next = NULL;
        temp = NULL;
    }
}

出队

void Queue300::deQueue300(Element300 input)



{
    NodePtr300 temp = NULL;

    if (rear == NULL && front == NULL)
    {
        cerr << "The queue is already empty, could not delete(dequeue) any more elements." << endl;
    }

    else if (front == rear)
    {
        strcpy(temp->element, input);
        temp = front;
        delete temp;
        temp = NULL;
        front = NULL;
        rear = NULL;
    }

    else
    {
        strcpy(temp->element, input);
        temp = front;
        front = front->next;
        temp->next = NULL;
        delete temp;
        temp = NULL;
    }
}

【问题讨论】:

    标签: c++ linked-list queue


    【解决方案1】:

    在 enqueue 中,当你说“temp = rear->next”时,你会覆盖 temp 中指向新节点的指针。

    将新节点添加到链表中时,通常最好先在新节点中设置指针:

    temp->next = null;
    rear->next = temp;
    rear=temp;
    

    还有:

    • 报错后,必须返回。如果你继续下去,你会崩溃的。最好是抛出异常或者返回错误码

    • 出队中的 strcpys 走错路了

    • 为防止缓冲区溢出,您应该真正使用 strncpy 而不是 strcpy,然后确保目标以 null 结尾,因为 Element 应该是一个字符串

    【讨论】:

      【解决方案2】:

      我们来看看Enqueue:

          if (temp == NULL)
          {
              ...
          }
          else if (front == NULL && rear == NULL)
          {
              ...
          }
          else
          {
              strcpy(temp->element, input); // copy input into temp->element
      
              // temp points to rear->next (it is always NULL, isn't it?)
              // we lost the Node300 previously pointed by temp.
              temp = rear->next; 
      
              // rear points to the same location as temp (NULL)
              rear = temp;
      
              // rear->next: dereferencing NULL /!\
              rear->next = NULL;
      
              temp = NULL;
          }
      

      ASCII 艺术的时间。这是在入队之前:

        REAR -------------------
                               |
                               v
                [0]--> ... -->[N]-->NULL
                 ^
        FRONT ---|
      

      你分配了一个节点[X],被temp引用:

      temp --> [X]
      

      REAR 的 next 必须指向同一个节点:

      REAR
       |   
       v
      [N] --> [X]
               ^
               |
              temp
      

      然后,必须更新 REAR 以引用 [X]。 就指针操作而言,您甚至不需要 temp 指针,但让我们保留它,因为您正在检查之前是否正确分配了节点,这很好。

      rear->next = temp;
      rear = temp;       // or rear = rear->next;
      temp->next = NULL; // or rear->next = NULL;
      

      还要注意,您在两个 else 分支中都执行了 strcpy(temp-&gt;element, input);。您可以在 temp == NULL 时从函数返回并在检查 front 和 rear 是否都为 NULL 之前进行复制。

      【讨论】:

      • 好吧,我在链表方面有点麻烦。因此,如果我将其更改为: strcpy(temp->element, input);后方=温度;后方->下一个 = NULL;临时=空;这样会更好吗?
      • @jacksbta 但是 old-rear 的 next 指针仍然指向 null ;-) 查看编辑
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 2013-07-07
      相关资源
      最近更新 更多