views:

76

answers:

1

I keep getting the first entry appended 4 times instead of one time.. when I append my first entry to the Queue it appends it 4 times..I thought this might be the problem..but it looks like it isn't. I can't find where the problem is..

I also created a print function for the nodes, and it showes that there are 4 of the same entries in the queue, so it is not a printing problem. And it doesn't look like it's in the read function. Maybe it's in the logic of the append function?? Still working on it..

This is the output: 3X^2 + 3X^2 + 3X^2 + 3X^2 + 1 but it should be 3X^2 + 1

This is my append function:

//Append(Add) item to back of queue.
Error_code Extended_queue::append(const Queue_entry &item) {
    Node<Queue_entry> *new_rear = new Node<Queue_entry>(item);

    if(rear == nullptr){
        front = new_rear; // I also tried rear = new_rear; front = rear;            rear = new_rear;        
    }
    else {
        rear->next = new_rear;
        rear = new_rear;
    }

    return success;
}

And here is the code that prints the output:

This is the node code declaration:

#ifndef NODE_H
#define NODE_H

enum Error_code{success,underflow,overflow}; // Used in node containing classes

template <class Node_entry> // Template to allow for more varience

// Part of a linked structure
struct __declspec(align(1)) Node{
    Node_entry entry; // Data contained in the node
    Node *next;   //Pointer to next node
    //constructors
    Node(); // Creates empty node
    Node(Node_entry item, Node *add_on = nullptr); // Creates node with specified data and pointer to next node
};

/* Post: The Node is initialized to contain nothing, and to have a null pointer.*/
template <class Node_entry>
Node<Node_entry>::Node()
{
    entry = nullptr;
    next = nullptr;
}

/* Post: The Node is initialized to contain item, and to point to add_on.*/
template <class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node *add_on)
{
    entry = item;
    next = add_on;
}


#endif
A: 

It looks like the copy constructor had bad logic. After I fixed th constructor, the driver only returned the first term as front and rear entry. So I had to fix up the overloaded = operator as well.

New Code(for copy constructor):

Extended_queue::Extended_queue(const Extended_queue &original){
    Node<Queue_entry> *temp_node, *original_node = original.front;
    if(original.empty()){ //original queue is empty, set new to NULL        
        front = nullptr;
        rear = nullptr;
    }
    else
    {
        front = temp_node = new Node<Queue_entry>(original_node->entry,nullptr);
        while(original_node->next != nullptr)
        {
            original_node = original_node->next;
            //needed to change next and still incriment
            temp_node->next = new Node<Queue_entry>(original_node->entry, nullptr);
            temp_node = temp_node->next;
            //rear->next = temp_node;           
            //rear = temp_node;

        }
        rear = temp_node->next;
    }
}
Knownasilya