模板队列得到错误的结果

时间:2013-10-26 15:05:24

标签: c++

我正在编写一个c ++代码来实现模板队列,但是当我运行我的代码时,我得到错误的结果,而不是预期的结果,这里是头文件和主代码。

queue.h

#include <iostream>
#pragma once
using namespace std;
typedef int Error_code;
#define SUCCESS 0
#define OVERFLOW -1
#define UNDERFLOW -2


template <class T>
class Node{
public:
    T item;
Node * next;
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL;}
};

template <class T>
class queue
{
protected:
    Node *front, *rear;
    int count;      

public:
    queue(){
    cout<<"constructor \n";
    count = 0;
    front = rear = NULL;};                                      
    ~queue(){Node<T> * p;                       
    while(front != NULL)            
    {
        p = front;
        front = front->next;            
        delete p;                   
    }};
    bool isempty(){
    //return count == 0;
    if(front==NULL)
        return true;
    else 
        return false;
    };
    bool isfull(){return false;};
    Error_code serve(){
    Error_code outcome = SUCCESS;
    Node<T> *p;
    if(isempty()){
        cout<<"empty queue\n";
        outcome=UNDERFLOW;
    }
    else{
    p=front;
    front=front->next;
    delete p;
    count--;
    }
    return outcome;
    } ;
    Error_code retrieve(T &item){
    Error_code outcome= SUCCESS;
    if(isempty())
    {           // front node is empty, queue is empty
        //return false;
        cout<<"empty queue\n";
        outcome=UNDERFLOW;
    }
    return outcome;
    };      


    Error_code append(T item){


    Node<T> * n ;
    n= new Node;                // create node
    n->item = item;                 // set node pointers
    n->next = NULL;     


    if (isempty())      
        {
            rear=front = n; 
        }
    else
    {
        rear->next = n;             // else place at rear
    rear = n;                           // have rear point to new node
    }
    count++;
    return SUCCESS;
    };                                          
};

的main.cpp

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


int main()
{
    queue<int> q;
    queue<char> q2;
    int x;
    char y;
    cout<<"the fisrt queue is :";
    q.isempty()?cout<<"empty \n\n":cout<<"not empty \n\n";
    cout<<"the second queue is :";
    q2.isempty()?cout<<"empty \n\n":cout<<"not empty \n\n";
    q.append(2);
    q.append(3);
    q.append(4);

    q2.append('a');
    q2.append('b');
    q2.append('c');

    q.retrieve(x);
    cout<<x<<endl;
    q.serve();

    q.retrieve(x);
    cout<<x<<endl;
    q.serve();

    q.retrieve(x);
    cout<<x<<endl;
    q.serve();

    q2.retrieve(y);
    cout<<y<<endl;
    q2.serve();

    q2.retrieve(y);
    cout<<y<<endl;
    q2.serve();

    q2.retrieve(y);
    cout<<y<<endl;
    q2.serve();

    q.retrieve(x);
    q2.retrieve(y);
    system("pause");
    return 0;

}

我得到了这些结果:

构造函数

构造

第一个队列是:空

第二个队列是:空

-858993460

-858993460

-858993460

然后它打印一些符号

空队列

空队列

1 个答案:

答案 0 :(得分:1)

您的retrieve函数无法检索:

Error_code retrieve(T &item){
    Error_code outcome= SUCCESS;
    if(isempty())
    {           // front node is empty, queue is empty
        //return false;
        cout<<"empty queue\n";
        outcome=UNDERFLOW;
    }else
    {
        // needs something like:
          item = front->item;
    }

    return outcome;
};

除了缺少模板参数外,它应该可以正常工作。