I need some help with my linked list. I think the problem is in the copy constructor or the assignment overload. it keeps giving my segmentation fault when i call:
(Queue test_int_copy(test_int);)
Any other error or poor implementation that you can see would also be very helpful.
the .h file
#ifndef QUEUE_H
#define QUEUE_H
template <class Object>
class Queue
{
public:
Queue();
//initializes empty queue
Queue(const Queue& a_queue);
//copy constructor
Queue& operator =(const Queue& rhs);
//overload assignment operator
bool enqueue(const Object& d);
//insert object into queue,return true
//return false if not
bool dequeue(Object& d);
//remove object from queue,return true
//if empty return false
bool isEmpty();
//check if empty
~Queue();
//destructor
private:
struct ListNode
{
Object obj;
//object that is in the list
ListNode *next;
//pointer to the next node
};
//struct of list
ListNode *head;
//pointer that points to head
};
#endif //Queue_H
#include "queue.cpp" //include queue.cpp with file
the .cpp file
#include <iostream>
using namespace std;
template <class Object>
Queue<Object>::Queue()
{
head = NULL;
}
template <class Object>
Queue<Object>::Queue(const Queue<Object> &a_queue)
{
head = NULL;
ListNode *nodePtr = a_queue.head;
while (nodePtr){
enqueue(nodePtr->obj);
nodePtr = nodePtr->next;
}
}
template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
//head = NULL;
ListNode *nodePtr = rhs.head;
Object temp;
while(head){
dequeue(temp);
}
while (nodePtr){
enqueue(nodePtr->obj);
nodePtr = nodePtr->next;
}
}
template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
ListNode *newNode = new ListNode;
newNode->obj = d;
newNode->next = NULL;
ListNode *nodePtr = NULL;
ListNode *previousNode = NULL;
if(isEmpty()){
head = newNode;
return true;
}
else{
nodePtr = head;
while(nodePtr != NULL){
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if(previousNode->next == NULL){
previousNode->next = newNode;
return true;
}
else
return false;
}
}
template <class Object>
bool Queue<Object>::dequeue (Object& d) //Dequeue
{
ListNode *nodePtr;
if(!head)
return false;
else{
if(head->next != NULL){
nodePtr = head;
d = nodePtr->obj;
head = head->next;
delete nodePtr;
return true;
}
else{
d = head->obj;
head = NULL;
return true;
}
}
}
template <class Object>
bool Queue<Object>::isEmpty() //Check if Empty
{
if(!head)
return true;
else
return false;
}
template <class Object>
Queue<Object>::~Queue() //Destructor
{
Object temp;
while(head)
dequeue (temp);
}
Also don't include the source file like that. Or if you insist, make sure to put it inside the include guard. e.g.
#include "queue.cpp" //include queue.cpp with file
#endif //Queue_H
Otherwise if the header is included multiple times (which happens all the time), you'll have a compilation error. (STuff will be defined multipled times).
Also, if you're going to include the "source" from the header -- make sure all functions are "inline" (implicitly or explicitly), otherwise you'll get linker errors
You should initialize your head pointer in your copy constructor (and also in your operator = function by the way). If you don't do so, head might end up with an undefined value and fail the IsEmpty test, witch may lead to a crash in enqeue.
One mistake I can see: in the dequeue() method, you do not set head to NULL after removing the last item.
Also, in the assignment overload:
while(head){
dequeue(temp);
head = head->next;
}
... You do not need to set head = head->next, that has already been done by the dequeue() method.
Your enqueue() function always inserts a node in the end. So it's better to maintain an extra member variable as: ListNode *tail;pointing to the last node of the list. So when you are adding a node you don't need to traverse through whole list and simply update the *tail. This will save lot of time.
For whatsoever reason you return false from enqueu(), do NOT forget to delete newNode; otherwise it will leak memory.
isEmpty() you can simplify as bool isEmpty() { return (0 != head); }.
Instead of declaring & passing a raw object in dequeue(), you can simply return Object* if element is removed or return 0 (NULL) if it's empty; that's equivalent to true and false.
Importantly, whenever you delete head; always do head = 0;. Because that's a member variable and should not be dangling.
Related
I read some of the other posts on this topic because there were quite a few, but they didn't really help my situation.
I am getting memory leaks in my implementation of a doubly linked list. I have to make my own so using list is not an option.
here are the two push functions I am using...
template <class T>
void dllist<T>::push_front(T val) {
node* new_node = new node;
new_node->value = val;
new_node->forward = head;
new_node->backward = nullptr;
if (head != nullptr)
head->backward = new_node;
head = new_node;
}
and...
template <class T>
void dllist<T>::push_back(T val) {
node* new_node = new node;
new_node->value = val;
new_node->forward = nullptr;
if (!head)
head = new_node;
else {
node* traveller = head;
while (traveller->forward != nullptr)
traveller = traveller->forward;
traveller->forward = new_node;
new_node->backward = traveller;
}
}
finally, here is my destructor
template <class T>
dllist<T>::~dllist() {
node* current = head;
while (current != nullptr) {
node* forward = current->forward;
delete current;
current = forward;
}
}
In main, I declare an object of type dllist called mylist and I make a few calls to push_front with some integer values and then push_back.
I am using the CRT library to check for leaks and there is a leak at each call to push_back or push_front.
I am confused because I thought I made my destructor correctly. Is there something else Im not seeing?
If anyone could point me in the right direction I'd appreciate it!
Thanks.
MRE
template<class T>
class dllist {
struct node {
T value;
node* forward;
node* backward;
};
node* head;
public:
dllist(); // default constructor
~dllist(); // default destructor
void push_front(T); // push element to the front of the list
void push_back(T); // push element to the back of the list
};
int main() {
{
dllist<int> mylist;
mylist.push_front(10);
mylist.push_front(12);
mylist.push_front(14);
mylist.push_front(16);
mylist.push_front(18);
mylist.push_front(19);
mylist.push_back(11);
mylist.push_back(21);
mylist.push_back(31);
mylist.push_back(41);
mylist.push_back(31);
mylist.push_back(41);
mylist.push_back(222);
}
_CrtDumpMemoryLeaks();
return 0;
}
template <class T>
dllist<T>::dllist() {
head = nullptr;
}
So I'm trying to create a Linked List class to understand better how pointers and data structures work but I keep running into a -11 SIGSEGV error. When I looked up the error it said that I might be using dereferenced pointers or accessing an array out of its bounds but neither of those makes sense for my program. I've searched everywhere for similar issues but none of them seem to apply to my program. Can anyone else see what I'm doing wrong?
#include <stdexcept>
#pragma once
using namespace std;
#define NODE typename LinkedList<T>::Node*
template <typename T>
class LinkedList {
public:
void AddHead(const T& data); //Adds new node to the beginning of the list
void AddTail(const T& data); //Adds new node to the end of the list
LinkedList(); //Default constructor
LinkedList(const LinkedList<T>& list); //Copy constructor
struct Node {
/*Individual node that stores the data*/
T data;
Node* prev;
Node* next;
Node(); //Default constructor for node
Node(T _data); //Data constructor for node
Node(T _data, Node* _prev, Node* _next); //Full constructor for node
};
private:
NODE head = nullptr;
NODE tail = nullptr;
unsigned int count;
};
/*Function definitions*/
template <typename T>
void LinkedList<T>::AddHead(const T& data) {
NODE tempRef = new Node(data, nullptr, head);
head->prev = tempRef;
head = tempRef;
delete tempRef;
count++;
}
template <typename T>
void LinkedList<T>::AddTail(const T& data) {
NODE tempRef = new Node(data, tail, nullptr);
tail->next = tempRef;
tail = tempRef;
delete tempRef;
count++;
}
template <typename T>
LinkedList<T>::LinkedList() {
count = 0;
head = nullptr;
tail = nullptr;
}
template <typename T>
LinkedList<T>::LinkedList(const LinkedList<T>& list) {
this->head = list.head;
this->tail = list.tail;
this->count = list.count;
}
/*Node Constructors*/
template <typename T>
LinkedList<T>::Node::Node() {
next = nullptr;
prev = nullptr;
}
template <typename T>
LinkedList<T>::Node::Node(T _data) {
next = nullptr;
prev = nullptr;
data = _data;
}
template <typename T>
LinkedList<T>::Node::Node(T _data, Node* _prev, Node* _next) {
next = _next;
prev = _prev;
data = _data;
}
The both functions AddHead and AddTail have a serious bug because the allocated node is at once deleted
head = tempRef;
delete tempRef;
and
tail = tempRef;
delete tempRef;
So the pointers head and tail have invalid values.
Moreover the functions do not update tail and head correspondingly in each function.
And initially the both pointers are equal to nullptr. So these statements
head->prev = tempRef;
and
tail->next = tempRef;
result in undefined behavior.
The function AddHead can be defined the following way
template <typename T>
void LinkedList<T>::AddHead(const T& data) {
NODE tempRef = new Node(data, nullptr, head);
if ( head == nullptr )
{
head = tail = tempRef;
}
else
{
head = head->prev = tempRef;
}
count++;
}
And the function AddTail can look like
template <typename T>
void LinkedList<T>::AddTail(const T& data) {
NODE tempRef = new Node(data, tail, nullptr);
if ( tail == nullptr )
{
tail = head = tempRef;
}
else
{
tail = tail->next = tempRef;
}
count++;
}
The copy constructor (and the copy assignment operator) either should be defined as deleted or shall make a deep copy of the list passed as the argument.
Otherwise two lists will try to delete the same nodes two times (in the forgotten by you destructor).
The structure Node should be declared as a private class member.
You delete the nodes you add to the list in both AddTail and AddHead. This leaves pointers to garbage in the list.
Also, it's not clear how to use your linked list. You cannot call AddHead if head is nullptr (because AddHead dereferences head) and you cannot call AddTail if tail is nullptr. Since your constructor sets both head and tail equal to nullptr, what can you do next?
If it's legal for head to be nullptr, why does AddHead do head->prev = tempRef; without checking if head is nullptr?
I would strongly urge you to document your code. For example, what are the required preconditions for AddHead to be called? Is it supposed to be safe to call if head is nullptr or is it a requirement that it not be? Why is that not documented?
I'm new to C++ and have been trying to implement a Singly Linked List, that provides an implementation the destructor, copy constructor and assignment operator. I'm running into compilation issues when trying to implement the copy constructor and the assignment operator.
Here's node.hpp
#ifndef LINKED_LIST_NODE_HPP
#define LINKED_LIST_NODE_HPP
template <typename T>
class Node{
public:
T data;
Node* next;
Node();
Node(T);
Node(const Node&);
~Node();
};
template <typename T>
Node<T>::Node(){}
template <typename T>
Node<T>:: Node(const T data): data(data), next(nullptr){}
template <typename T>
Node<T>::Node(const Node<T>& source) : data(source.data),
next(new Node)
{
(*next) = *(source.next) ;
}
template <typename T>
Node<T>::~Node(){}
#endif //LINKED_LIST_NODE_HPP
This is singly_linked_list.hpp
#ifndef LINKED_LIST_SINGLYLINKEDLIST_HPP
#define LINKED_LIST_SINGLYLINKEDLIST_HPP
#include <iostream>
#include "node.hpp"
template <typename T>
class SinglyLinkedList {
private:
Node<T>* head;
std::size_t count;
public:
SinglyLinkedList();
SinglyLinkedList(const SinglyLinkedList& source);
SinglyLinkedList& operator=(const SinglyLinkedList& source);
~SinglyLinkedList();
void insert(T);
void remove(T);
bool isEmpty();
int length();
void print();
};
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList() : head(nullptr), count(0){}
template <typename T>
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& source){
Node<T>* curr = source.head;
while(curr != nullptr){
Node<T>* p = new Node<T>;
p->data = curr->data;
curr = curr->next;
}
}
//template <typename T>
//SinglyLinkedList<T>::SinglyLinkedList& operator=(const SinglyLinkedList<T>& source){
// //not sure how to implment this.
//}
template <typename T>
SinglyLinkedList<T>::~SinglyLinkedList() {
if(!isEmpty()){
Node<T>* temp = head;
Node<T>* prev = nullptr;
while(temp->next != nullptr){
prev = temp;
temp = temp->next;
delete prev;
}
delete temp;
}
}
template <typename T>
bool SinglyLinkedList<T>::isEmpty() {
return head == nullptr;
}
template <typename T>
void SinglyLinkedList<T>::insert(T item) {
Node<T>* p = new Node<T>(item);
p->next = head;
head = p;
count += 1;
}
template <typename T>
void SinglyLinkedList<T>::remove(T item) {
bool present = false;
if (head->data == item){
Node<T>* temp = head;
head = head->next;
delete(temp);
count -= 1;
return;
}
Node<T>* temp = head;
while (temp->next != nullptr){
if (temp->next->data == item){
Node<T>* removable = temp->next;
temp->next = temp->next->next;
delete(removable);
present = true;
count -= 1;
break;
} else{
temp = temp->next;
}
}
if(!present){
throw std::invalid_argument("item not present in list");
}
}
template <typename T>
int SinglyLinkedList<T>::length() {
return count;
}
template <typename T>
void SinglyLinkedList<T>::print() {
if(isEmpty()){
throw std::invalid_argument("Can't print an empty list!");
}
Node<T>* temp = head;
while(temp != nullptr){
if(temp->next != nullptr){
std::cout<<temp->data;
std::cout<<"->";
}else{
std::cout<<temp->data;
}
temp = temp->next;
}
std::cout<<std::endl;
}
#endif //LINKED_LIST_SINGLYLINKEDLIST_HPP
I've commented out the copy constructor code to make this compile. What is the correct way of doing this? I'm just learning C++.
One issue that introduces complexity is that it is not well defined what the copy constructor of a node should do? Should the next field of the copy point to the next of the original, or it should create a copy of the next and point to that? The former is inadequate and error-prone, the latter would recursively create a copy of the whole list, one node at a time. This will work for lists of small size but will cause stack overflow for lists with many elements due to the depth of the recursive calls.
So to keep things simple, I wouldn't bother with copy constructor of a node.
template <typename T>
class Node {
public:
T data;
Node* next = nullptr;
Node() = default;
Node(const Node&) = delete; // since copying is not well defined, make it impossible to copy a node.
};
Copying a list is a well defined operation, so implementing the copy constructor makes sense. A mistake with your current implementation is that you allocate a new node, only to leak it later (nothing keeps track of the newly allocated node p). What you need looks more like this:
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList<T>& source)
: head(nullptr)
, count(0)
{
// deal with the trivial case of empty list
if (source.head == nullptr)
return;
// deal with the case where count >= 1
head = new Node<T>;
head->data = source.head->data;
head->next = nullptr;
count = 1;
Node<T>* lastCopied = source.head; // last node to be copied
Node<T>* lastAdded = head; // last node to be added to the current list
while (lastCopied->next != nullptr)
{
// create new node
Node<T>* p = new Node<T>;
p->data = lastCopied->next->data;
p->next = nullptr;
// link the newly created node to the last of the current list
lastAdded->next = p;
lastAdded = p;
// advance lastCopied
lastCopied = lastCopied->next;
count++;
}
}
Now regarding the assignment operator, luckily you can use the 'copy and swap' idiom that greatly simplifies things.
template <typename T>
SinglyLinkedList<T>& SinglyLinkedList<T>::operator =(SinglyLinkedList<T> source) // note that you pass by value.
{
std::swap(head, source.head);
std::swap(count, source.count);
return *this;
}
My answer would become too long if I tried to explain the copy and swap technique. It is a clever trick to write exception safe code and avoid duplication (implements assignment by using the copy ctor) at the same time. It is worth reading about it here.
Btw, the declaration of your class should look like this
template <typename T>
class SinglyLinkedList
{
private:
Node<T>* head = nullptr;
std::size_t count = 0;
public:
SinglyLinkedList(const SinglyLinkedList& source);
SinglyLinkedList& operator=(SinglyLinkedList source);
// other members here...
};
PS. My code assumes you are using c++11 or a later standard.
I don't like the direction this is headed. I'm going to explain how to do this approach right because it is an excellent lesson on recursion, but because it's recursion it can run the program out of Automatic storage (march off the end of the stack, most likely) with a sufficiently large list. Not cool.
The logic:
Copying a node copies the next node if there is one. This looks something like
template <typename T>
Node<T>::Node(const Node<T>& source) : data(source.data)
{
if (source.next) // if there is a next, clone it
{
next = new Node<T>(*source.next);
}
else
{
next = nullptr;
}
}
This reduces the linked list copy constructor to
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& source){
head = new Node<T>(*source.head); //clone the head. Cloning the head will clone everything after
count = source.count;
}
A helper function may, uh... help here to make the Node copy constructor a bit more idiomatic
template <typename T>
Node<T> * initnext(const Node<T> & source)
{
if (source.next)
{
return new Node<T>(*source.next);
}
else
{
return nullptr;
}
}
template <typename T>
Node<T>::Node(const Node<T>& source) : data(source.data),
next(initnext(source))
{
}
but I don't think you gain much.
So... I don't like the above. What would I do instead? Something a lot like opetroch's solution above, but different enough that I'll write this up.
The node stays brutally stupid. As far as I'm concerned all a Node should ever know is how to store the payload and find other Nodes. This means the linked list should do all of the heavy lifting.
Concept 1: head is nothing but a next pointer. Once you abstract away its differences, unimportant here, you can use it exactly the same way you would next.
Concept 2: If you only know where next points, you have to do a bunch of extra book-keeping to track the previous node to update it's next. But if you take advantage of the previous's next pointing to the current node, you can throw out even more code. By tracking the previous node's next you have all of the information you need.
Concept 3: If you keep a pointer to the previous node's next, you can update that previous node's next any time you want by dereferencing it.
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& obj)
{
Node<T>* tocopy = obj.head;
Node<T>** nextpp = &head; // head is a next. We are now pointing to a pointer to next
while (tocopy) // keep looping until there is no next node to copy
{
*nextpp = new Node<T>(tocopy->data); // copy source and update destination's next
nextpp = &(*nextpp)->next; // advance to point at the next of the node we just added
tocopy= tocopy->next; // get next node to copy
}
count = obj.count;
}
Because this iterates rather than recurses it doesn't eat up Automatic storage (probably the stack) and can keep going until the cows come home.
This logic can also be applied to remove
template <typename T>
void SinglyLinkedList<T>::remove(T item) {
Node<T>** temp = &head; //head is nothing but a next pointer.
// by pointing to where the next is, we don't
// need to track a previous or have special handling
// for the head node
while (*temp){ // because we now have a pointer to a pointer, we need an
// extra dereference
if ((*temp)->data == item){
Node<T>* removable = *temp;
*temp = (*temp)->next;
delete(removable);
count -= 1;
return; // no need for any special magic. Just get out.
} else{
temp = &(*temp)->next; // update the pointer to the next
}
}
// if we got here the node was not found.
throw std::invalid_argument("item not present in list");
}
And following through on head is just a next, we can also gut the destructor:
template <typename T>
SinglyLinkedList<T>::~SinglyLinkedList() {
while(head){ // if head null, list empty
Node<T>* temp = head; // cache so we can delete
head = head->next; // move head
delete temp; //delete removed node
}
}
I'm building my own linked list class and I'm having some issues figuring out how to write some functions to help me traverse this list. This is my first time building a linked list from scratch, so if my approach is unconventional please let me know what might be more conventional.
I'd like write a function, within the List class that allows me to increment to the next element called getNext() as well as one that getPrev();
I wrote getNext like this:
T* getNext(){return next;}
However it tells me next is not declared within the scope. I'd also like to write a function that lets me access and modify the object within the list. I was considering using the bracket operator, but first I need to write a function to return the data member. Perhaps If I take a similar approach as I did within my pop functions.. thinking about it now. However, I'd still appreciate any advice.
Here is my List class:
#ifndef LIST_H
#define LIST_H
//List Class
template <class T>
class List{
struct Node {
T data;
Node *next;
Node *prev;
//Constructs Node Element
Node(T t, Node* p, Node* n) { data = (t); prev = (p); next = (n); }
// T *getNext() {return next;}
};
Node *head;
Node *tail;
public:
//Constructor
List() { head = NULL; tail=NULL; }
//Destructor
~List() {
while(head){
Node * temp(head);
head = head->next;
delete temp;
}
}
//is empty
bool empty() const {return (!head || !tail ); }
operator bool() const {return !empty(); }
//Push back
void push_back(T data) {
tail = new Node(data, tail, NULL);
if(tail->prev) //if the node in front of tail is initilized
tail->prev->next = tail;
if( empty() )
head = tail;
}
//Push front
void push_front(T data) {
head = new Node(data, NULL, head);
if(head->next)//if the node following head is initilized
head->next->prev = head;
if( empty() )
tail = head;
};
T pop_back() {
if( empty() )
throw("Error in List: List is empty\n");
Node* temp(tail);
T data(tail->data);
tail = tail->prev;
if( tail )
tail->next = NULL;
else
head = NULL;
delete temp;
return data;
}
T pop_front() {
if (empty())
throw("Error in List: List is empty\n");
Node* temp(head);
T data(head->data);
head = head->next;
if(head)
head->prev=NULL;
else
tail = NULL;
delete temp;
return data;
}
T getNext(){return next;}
};
#endif
getNext should be part of the struct Node and return a Node*
Node* getNext() { return next; }
Then from that you can get the value.
If you have to have it part of the list itself, which I would not recommend it will need to take a parameter of what Node you would like the next of:
Node* getNext(Node* n) {return n->next;}
Again, I recommend the first option.
Here is an approximate whole class with both of these:
template<typename T>
class List {
public:
struct Node {
Node* next, prev;
T data;
//some constructor and stuff
Node* Next() {return next;}
}
//some constructors and other functions
Node* getNext(Node* _n) {return _n->Next();}
}
then to use:
int main() {
List<int> l;
//add some stuff to the list
//get the head of the list
List<int>::Node* head = l.head; //or some corresponding function
//then
List<int>::Node* next = head->Next();
//or
List<int>::Node* next2 = l.getNext(head);
}
for starters getNext() should not return a pointer to the template class, it should return a pointer to the Node structure.
So it should be
Node* getNext(){return next;}
Because it's a member of Node struct and getNext is member of List. You should access it from an object of type Node.
I need some help making this program for class. We are working with g++ (linux? its through putty on a server) I am having a lot of issue with this linked list. The current error that it is giving me is
queue.cpp:2: error: expected unqualified-id before âusingâ
Wondering if anyone could help me figure it out. A little bit of searching shows that the problem seems to be in the #define somewhere? The error shows that it is in the .cpp file but i think it is in the .h file. Also if you could give me any programing advise about anything that seems off, wrong or if there a better way of doing it.
the queue.h file below
#ifndef QUEUE_H
#define QUEUE_H
template <class Object>
class Queue
{
public:
Queue();
Queue(const Queue& a_queue);
Queue& operator =(const Queue& rhs);
bool enqueue(const Object& d);
bool dequeue(const Object& d);
bool isEmpty() const;
~Queue();
private:
struct ListNode
{
Object obj;
ListNode *next;
};
ListNode *head;
}
#endif //Queue_H
#include "queue.cpp" //include queue.cpp with file
the queue.cpp file here.
#include <iostream>
using namespace std;
template <class Object>
Queue<Object>::Queue()
{
head = NULL;
}
template <class Object>
Queue<Object>::Queue(const Queue<Object>& a_queue)
{
head=NULL;
*this=a_queue;
}
template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
ListNode *nodePtr;
nodePtr = rhs.head;
if(this != rhs){
this.head = NULL;
while(nodePtr != NULL){
this.enqueue(nodePtr->obj);
nodePtr = nodePtr->next;
}
}
}
template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode;
previousNode = NULL;
nodePtr = NULL;
newNode = new ListNode;
newNode->obj = d;
newNode->next = NULL;
if(isEmpty){
head = newNode;
return true;
}
else{
nodePtr = head;
previousNode = NULL;
while(nodePtr != NULL){
previousNode = nodePtr;
nodePtr=nodePtr->next;
}
if(previousNode->next == NULL){
previousNode->next = newNode;
return true;
}
else
return false;
}
}
template <class Object>
bool Queue<Object>::dequeue (const Object& d) //Dequeue
{
ListNode *nodePtr;
if(!head)
return false;
else{
if(head->next != NULL){
nodePtr = head;
d=nodePtr->obj;
head = head->next;
delete nodePtr;
}else
delete head;
return true;
}
}
template <class Object>
bool Queue<Object>::isEmpty() const //Check if Empty
{
if(!head)
return true;
else
return false;
}
template <class Object>
Queue<Object>::~Queue() //Destructor
{
Object temp;
while (head != NULL)
dequeue (temp);
}
Don't include your implementation file from the header. Include the header from the implementation file.
I don't see you say "using namespace std" in the header in your code, but you say in your comment that you do that. Don't. Never say using namespace in a header file.
Place all template definitions in the header.
You're missing a semicolon in your class definition in the header.
Your assignment operator is not exception-safe. Make sure your copy-constructor is correct and then use the copy & swap idiom.
Your copy-constructor is incorrect. If you want to support lazy copy (copy on write) then that's fine, but you're missing the actual deep copy operation. Be extra-careful with the copy constructor, because it extremely important you get it right.
You need a semicolon after your class declaration in the header.
class Queue
{
public:
Queue();
Queue(const Queue& a_queue);
Queue& operator =(const Queue& rhs);
bool enqueue(const Object& d);
bool dequeue(const Object& d);
bool isEmpty() const;
~Queue();
private:
struct ListNode
{
Object obj;
ListNode *next;
};
ListNode *head;
};