I am trying to implement Queue container in C++ based on a linked list. I used the same structure to implement Stack and it worked fine.
But now I havet trouble with the method "enqueue". I can't understand what exactly is the problem, although I know that pointers are my weak point.
#include <iostream>
template <class N>
class node {
public:
N data;
node* next;
};
template <class Q>
class my_queue {
protected:
node<Q>* m_head;
unsigned int m_size;
public:
my_queue() {
m_head = NULL;
m_size = 0;
}
void enqueue(Q value) {
node<Q>* newel = new node<Q>; // creating the new element
node<Q>* last = m_head; // find the last element in the queue
while(last != NULL) {
last = last->next;
}
newel->data = value;
newel->next = last->next;
last->next = newel;
m_size++;
}
void print() {
node<Q>* element = m_head; // element == each element in the list
while(element != NULL) {
std::cout << element->data << std::endl;
element = element->next;
}
}
};
If I compile this with:
main() {
my_queue<int> q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.print();
return 0;
}
I get no errors, but when I run it I get "Segmentation fault".
lastwill bem_head, which was never allocated, so you cannot dolast->next = newel.