-1

我自己实现了一个队列。在测试时,我希望通过测试尾指针是否指向头节点来在队列为空时重新出队。但是,当指针确实指向头时,头节点的地址和一个指针点的地址是不同的,因此即使队列中没有任何内容,队列也不会停止出队。这让我很困惑。对此有何建议?多谢。

//header
#ifndef QUEUE_H
#define QUEUE_H

struct node
{
int val;
struct node* next;

};

class queue
{
private:
    node head;
    node* tail;
public:
    queue();
    void enqueue(int val);
    void dequeue(int& holder, bool& v);
};





#endif
// queue.cpp
#include "queue.h"

using namespace std;
queue::queue()
{
    tail = &head;

}
void queue::enqueue(int val)
{
    node* tmp = new node;
    tmp->val = val;
    if(tail == &head)
    {
            tail = tmp;
            tmp->next = &head;
            head.next = tmp;
    }
    else
    {
            node* holder = head.next;
            head.next = tmp;
            tmp->next = &head;
            holder->next = tmp;
    }
}

void queue::dequeue(int& holder,bool& v)
{

    if(tail == &head)
   {
            v = false;
   }
    else
    {
            node* cur = tail;
            tail = tail->next;
            holder = cur->val;
            v = true;
            delete cur;

    }

}
//test.cpp
#include <iostream>
#include "queue.h"
using std::cout;
using std::endl;
int main()
{
int ary[] = {1,2,3,4,5};

queue myq;
for(int i = 0;i< sizeof(ary);i++)
{
    myq.enqueue(ary[i]);
}
int tmp;
bool flag;
for(int i = 0;i<=7;i++)
{
    myq.dequeue(tmp,flag);
    if(flag)
            cout<<"number is "<<tmp<<endl;
    else
            cout<<"queue empty"<<endl;
}
return 0 ;
}
4

2 回答 2

0

我可以为您提供确切的解决方案,但这绝对不会教您任何东西,您只需将其复制并粘贴到您的代码中,然后去做其他事情。因此,我将帮助您添加一行代码以显示 &head 的当前值是什么,从而帮助确定实际问题是什么......

将此行添加到入队(在函数顶部)和出队:

     std::cout << __FUNCTION__ << ": " << &head << std::endl;

它将打印函数的名称和地址。(您可能还需要包括 iostream)

请特别注意您获得了多少打印输出!

于 2013-01-26T21:50:11.427 回答
0

下面的工作代码:(学习使用调试器)

#include <iostream>
using namespace std;
//header
#ifndef QUEUE_H
#define QUEUE_H

struct node {
    int val;
    struct node* next;

};

class queue {
private:
    struct node* head;
    struct node* tail;
public:
    queue();
    void enqueue(int val);
    void dequeue(int& holder, bool& v);
};

#endif

using namespace std;
queue::queue() {
    tail = NULL;
    head = NULL;

}
void queue::enqueue(int val) {
    node* tmp = new node;

    tmp->val = val;

    if (head == NULL && NULL == tail) {//No nodes Empty
        head = tail = tmp;
        tmp->next = NULL;

    }else if (tail == head) { //Single node
        tail = tmp;
        tmp->next = head;
        head->next = tmp;
    } else {
        tail->next = tmp;
        tmp->next = head;
        tail = tmp;
    }
}

void queue::dequeue(int& holder, bool& v) {
    if (NULL == tail || NULL == head) {
        v = false;
        return;
    } else if (tail == head) {// single node
        holder = head->val;
        delete head;
        head = NULL;
        tail = NULL;
        v = true;
    } else { //more than one node
        //Go to the last but one node and chop the tail
        node* cur = head;
        while (cur->next != tail)
            cur = cur->next;

        cur->next = head;
        holder = tail->val;
        delete tail;
        tail = cur;
        v = true;
    }
    return;
}

using std::cout;
using std::endl;
int main() {
    int ary[] = { 1, 2, 3, 4, 5 };

    queue myq;

    for (int i = 0; i < 5; i++) {
        myq.enqueue(ary[i]);
    }

    int tmp;
    bool flag;

    for (int i = 0; i <= 7; i++) {
        myq.dequeue(tmp, flag);
        if (flag) {
            cout << "number is " << tmp << endl;
        }
        else {
            cout << "queue empty" << endl;
        }
    }
    return 0;
}
于 2013-01-26T22:57:22.390 回答