-
Trying to make a queue!
Self explanitory... I am getting access violations. It is close to working but alas...
QUEUE.H
Code:
#ifndef QUEUE_H
#define QUEUE_H
const int MAX = 5;
template <class T> struct node{
T value;
node<T>* ptr;
};
template <class T> class queue{
private:
int counter;
node<T>* head;
node<T>* tail;
public:
queue();
int count();
void enqueue(T newtop);
T dequeue();
T onTop();
bool isEmpty();
bool isFull();
};
template <class T> queue<T>::queue(){
counter = 0;
head = NULL;
tail = NULL;
}
template <class T> int queue<T>::count(){
return counter;
}
template <class T> void queue<T>::enqueue(T newtop){
node<T>* temp = new node<T>;
//srand(time(NULL));
if (!isFull()){
temp->value = newtop; //you may have to fill in other values.
cout << "\nInserting " << temp->value << endl;
//temp->ptr = head;
//head = temp;
temp = NULL;
tail -> ptr = temp;
tail = temp;
counter++;
}
else
cout << "Queue full: Cannot exceed " << MAX << " items\n";
}
template <class T> T queue<T>::dequeue(){
node<T>* dummyPtr;
if (!isEmpty()){
counter--;
dummyPtr = head;
T popValue = head->value;
head = head->ptr;
delete dummyPtr;
return popValue;
}
else{
cout << "\n<<Cannot dequeue empty queue, returning T()>>\n";
return T();
}
}
template <class T> T queue<T>::onTop(){
if (!isEmpty()){
T peekValue = head->value;
return peekValue;
}
else{
cout << "\n<<Cannot view top node of empty queue, returning T()>>\n";
return T();
}
}
template <class T> bool queue<T>::isEmpty(){
if (counter == 0){
return true;
}
else{
return false;
}
}
template <class T> bool queue<T>::isFull(){
if (counter == MAX){
return true;
}
else{
return false;
}
}
#endif
Next, we have the driver file
Code:
#include <iostream>
#include <conio>
#include "queue.h"
using namespace std;
int main(){
queue<int> q;
q.enqueue(1);
q.enqueue(2);
q.dequeue();
q.enqueue(3);
q.enqueue(4);
q.dequeue();
q.dequeue();
return 0;
}
Anyone willing to help me out; I'd really appreciate it
-
I think it may be the node-adding function
-
Try this.
Code:
template <class T> void queue<T>::enqueue(T newtop){
node<T>* temp = new node<T>;
//srand(time(NULL));
if (!isFull()){
temp->value = newtop; //you may have to fill in other values.
cout << "\nInserting " << temp->value << endl;
if (tail != NULL)
tail -> ptr = temp;
tail = temp;
tail -> ptr = NULL;
if (head == NULL)
head = temp;
counter++;
}
else
cout << "Queue full: Cannot exceed " << MAX << " items\n";
}
-
Or this.
Code:
template <class T> void queue<T>::enqueue(T newtop){
//srand(time(NULL));
if (!isFull()){
node<T>* temp = new node<T>;
temp->value = newtop; //you may have to fill in other values.
temp->ptr = NULL;
cout << "\nInserting " << temp->value << endl;
if (tail != NULL)
tail -> ptr = temp;
tail = temp;
if (head == NULL)
head = temp;
counter++;
}
else
cout << "Queue full: Cannot exceed " << MAX << " items\n";
}
-
Thanks! Did it have anything to do with dereferencing the null pointer?
-
>Thanks! Did it have anything to do with dereferencing the null pointer?
That's probably the reason. The first time you call the enqueue function, tail is NULL, so tail would be NULL here:
tail -> ptr = temp;