-1

我正在用 C++ 创建一个队列类,但无法让前端函数正常工作。它应该打印队列中第一个节点的值。我的 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 = node(item, NULL);
   if (front_p == NULL) //queue is empty
    {
       front_p = &newnode;
       back_p = &newnode;
    }
   else 
   {
       back_p->next = &newnode;
       back_p = &newnode;
   }
   current_size ++;
}

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

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

我的头文件(queue.h)在这里

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

测试程序(tester.cpp)

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

int main(int argc, char * const argv[])
{
    queue q1;
    q1.enqueue(5);
    cout << "front: " << q1.front() << endl;
    cout << "front: " << q1.front() << endl;
    cout << "front: " << q1.front() << endl;
    q1.enqueue(10);
    cout << "front: " << q1.front() << endl;
    cout << "front: " << q1.front() << endl;
    cout << "size: " << q1.size() << endl;
}

生成文件

all: tester

tester: queue.o tester.o
    g++ tester.o queue.o -o tester

tester.o: tester.cpp
    g++ -c tester.cpp

queue.o: queue.cpp queue.h
    g++ -c queue.cpp

clean:
    rm -f tester *.o

当我运行我的测试程序时,我得到了这个:

front: 5
front: 6299744
front: 6299744
front: 10
front: 6299744
size: 2

如您所见,在第一次入队之后,front 返回了它应该返回的值,即队列前面的值。但之后它返回一些奇怪的数字,我不知道它来自哪里!然后当我再次入队时,它再次打印正常。只有在两次调用 front 之后,它才会开始打印混乱的值。谁能帮我理解发生了什么?

4

1 回答 1

4

您的程序遇到未定义的行为,因为您有指向您不拥有的内存的指针:

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

在函数结束时,被newnode销毁,但仍然指向该内存位置。要么动态分配:front_pback_p

node* newnode = new node(item, NULL);

或使用std::shared_ptr<node>.

于 2012-10-25T08:25:08.547 回答