将队列项复制到另一个类对象中

时间:2016-03-26 11:50:44

标签: c++ queue

当我在队列中显示项目时,我使用while循环

while (!queue.isEmptyQueue())
{
    cout << queue.front() << " ";
    queue.deleteQueue();
}

由于deleteQueue()功能,这些项目将会消失,因此我无法再次显示它。 如何创建队列副本?

我尝试了下面的代码,但它给了我错误。

queue1 = queue;
while (!queue1.isEmptyQueue())
{
    cout << queue1.front() << " ";
    queue1.deleteQueue();
}

我正在使用linkedQueue.h

#include<assert.h>

template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};

template <class Type>
class linkedQueueType  //: public queueADT<Type>
{
    public:
        const linkedQueueType<Type>& operator=(const         linkedQueueType<Type>&);
        bool isEmptyQueue() const;
        bool isFullQueue() const;
        void initializeQueue();
        Type front() const;
        Type back() const;
        void addQueue(const Type& queueElement);
        void deleteQueue();
        linkedQueueType();
        linkedQueueType(const linkedQueueType<Type>& otherQueue);
        ~linkedQueueType();
    private:
        nodeType<Type> *queueFront; 
        nodeType<Type> *queueRear; 
};

template <class Type>
bool linkedQueueType<Type>::isEmptyQueue() const
{
    return(queueFront == NULL);
}

template <class Type>
bool linkedQueueType<Type>::isFullQueue() const
{
    return false;
} 

template <class Type>
void linkedQueueType<Type>::initializeQueue()
{
    nodeType<Type> *temp;
    while (queueFront!= NULL) 
    {
        temp = queueFront; 
        queueFront = queueFront->link; 
        delete temp; 
    }
    queueRear = NULL; 
} 

template <class Type>
void linkedQueueType<Type>::addQueue(const Type& newElement)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>; 
    newNode->info = newElement; 
    newNode->link = NULL; 
    if (queueFront == NULL) 
    {
        queueFront = newNode;
        queueRear = newNode;
    }
    else 
    {
        queueRear->link = newNode;
        queueRear = queueRear->link;
    }
}

template <class Type>
Type linkedQueueType<Type>::front() const
{
    assert(queueFront != NULL);
    return queueFront->info;
} 
template <class Type>
Type linkedQueueType<Type>::back() const
{
    assert(queueRear!= NULL);
    return queueRear->info;
} 

template <class Type>
void linkedQueueType<Type>::deleteQueue()
{
    nodeType<Type> *temp;
    if (!isEmptyQueue())
    {
    temp = queueFront; 
    queueFront = queueFront->link; 
    delete temp; 
    if (queueFront == NULL) 
    queueRear = NULL; 
    }
    else
    cout << "Cannot remove from an empty queue" << endl;
}

template<class Type>
linkedQueueType<Type>::linkedQueueType()
{
    queueFront = NULL; 
    queueRear = NULL; 
} 

template <class Type>
linkedQueueType<Type>::~linkedQueueType()
{
    initializeQueue();
}

2 个答案:

答案 0 :(得分:0)

你的问题是你的linkedQueueType没有赋值操作符,因此编译器会给你一个只复制指针的操作符。你需要一个克隆元素的赋值运算符。

答案 1 :(得分:0)

If what you want to do is copy the queue, then a copy constructor / assignment operator is what you should have, as Martin said.

But if what you want is for the items to still be there after you go through the queue, then what your queue is missing is a way to tell how many items are in the queue.

You can cout each item and then move it to the back of the queue, that number of times.

Either way, all of these are missing for it to be a good queue implementation. Copy constructor, assignment operator, size function.

(And technically, a queue doesn't have functionality to access the back item. So I wouldn't call this a queue with missing functionality, I would call it a linked list with missing functionality.)