0

I am writing a c++ code to implement template queue, but when I run my code, I get wrong results, not the expected results, here is the header file and the main code.

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;

}

I get these results:

constructor

constructor

the first queue is: empty

the second queue is: empty

-858993460

-858993460

-858993460

then it prints some symbols

empty queue

empty queue

4

1 回答 1

1

Your retrieve function doesn't 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;
};

Apart from the missing template arguments, it should work then.

于 2013-10-26T15:14:52.650 回答