Segmentation Faults, need help tracking a few down
Nov 7, 2012 at 12:08am UTC
Hello! I've written a simple Queue list with a linked list format. Unfortunately, I am getting several seg faults that I can't seem to track down.
Here is my Queue.h, where I believe they reside:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#ifndef QUEUE_H
#define QUEUE_H
#include "Node.h"
#include "QueueException.h"
using namespace std;
template <typename T>
class Queue
{
public :
Node<T>* head;
Node<T>* current;
int cnt;
Queue();
Queue(T data);
Queue(const Queue &rhs);
const Queue & operator =(const Queue &rhs);
~Queue();
void enqueue(T data);
T dequeue() throw (QueueException);
T front() throw (QueueException);
bool isEmpty();
};
template <typename T>
Queue<T>::Queue()
{
head = NULL;
current = NULL;
cnt = 0;
}
template <typename T>
Queue<T>::Queue(T data)
{
head = new Node<T>(data,NULL);
current = head;
cnt = 1;
}
template <typename T>
Queue<T>::~Queue()
{
Node<T>* curr = head;
while (current != NULL)
{
Node<T>* next = curr->next;
delete curr;
curr = next;
}
head = NULL;
}
template <typename T>
Queue<T>::Queue(const Queue<T> &rhs)
{
cnt = rhs.cnt;
head = NULL;
if (rhs.head != NULL)
{
head = new Node<T>(rhs.head->getData());
Node<T>* curr = head;
Node<T>* otherCurr = rhs.head->getNext();
while (otherCurr != NULL)
{
curr->setNext(new Node<T>(otherCurr->getData()));
curr = curr->getNext();
otherCurr = otherCurr->getNext();
}
}
}
template <typename T>
bool Queue<T>::isEmpty()
{
if (cnt == 0)
{
return 1;
}
else
{
return 0;
}
}
template <typename T>
T Queue<T>::front() throw (QueueException)
{
if (cnt == 0)
{
throw QueueException();
}
return head->data;
}
template <typename T>
T Queue<T>::dequeue() throw (QueueException)
{
T toRemove;
toRemove = head->getData();
head = head->getNext();
cnt--;
return toRemove;
}
template <typename T>
void Queue<T>::enqueue(T newdata)
{
if (head != NULL)
{
current = head;
while (current->getNext() != NULL)
{
current = current->getNext();
}
Node<T>* newinsert = new Node<T>(newdata, NULL);
current->setNext(newinsert);
}
else
{
Node<T>* newinsert = new Node<T>(newdata, NULL);
head = newinsert;
}
cnt++;
}
template <typename T>
const Queue<T>& Queue<T>::operator =(const Queue<T> &rhs)
{
cnt = rhs.cnt;
head = NULL;
if (rhs.head != NULL)
{
head = new Node<T>(rhs.head->getData());
Node<T>* curr = head;
Node<T>* otherCurr = rhs.head->getNext();
while (otherCurr != NULL)
{
curr->setNext(new Node<T>(otherCurr->getData()));
curr = curr->getNext();
otherCurr = otherCurr->getNext();
}
}
}
#endif
I know I need to learn to track these down myself, but I'm at my wit's end with these. Unfortunately, this block of code is a bit of an amalgamation of snippets from several previous programs (generic linked list template and its accompanying Node class).
Thank you for any help you can provide!
Last edited on Nov 7, 2012 at 12:48am UTC
Nov 7, 2012 at 3:17am UTC
Which function do the segmentation faults occur.
Nov 7, 2012 at 3:34am UTC
Node definition?
Memory leak in dequeue, btw.
Supplying a minimal 'main' in which the segmentation fault is reproducible would be good.
Topic archived. No new replies allowed.