模板队列得到错误的结果

template queue getting wrong results

本文关键字:错误 结果 队列      更新时间:2023-10-16

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

队列.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 queuen";
        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 queuen";
        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;
    };                                          
};

主.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 nn":cout<<"not empty nn";
    cout<<"the second queue is :";
    q2.isempty()?cout<<"empty nn":cout<<"not empty nn";
    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

然后它打印一些符号

空队列

空队列

您的retrieve函数无法检索:

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

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