Hi, I have trouble with the "enqueue" section of this code, can anyone here find the fault within my code.
I've been working on this for over 15+ hrs and I can't not find what I am doing wrong here. If there is anything else I need to provide, I'll do so ASAP.
PriorityQ has private variables: *frontPtr (Node) and count (int)
Node is a struct with: data (Message) and *nextPtr(Node)
Message is a class with priority (Priorities) and message (string)
Priorities contain = UNKNOWN, LOW, MEDIUM, HIGH

#include <iostream> 
#include <new>
#include "priorityq.h"

PriorityQ::PriorityQ()
{
	frontPtr=NULL;
	count=0;
}
PriorityQ::~PriorityQ()
{
	MakeEmpty();
	count=0;
}
void PriorityQ::MakeEmpty()
{
	Node* tempPtr;
	while(frontPtr!=NULL)
	{
		tempPtr=frontPtr;
		frontPtr=frontPtr->nextPtr;
		delete tempPtr;
		count--;
	}
}
void PriorityQ::Enqueue(Message msg)
{
	Node* curr;
	Node* prev;
	Node* temp1;
	Node* temp2;
	int num1=0, num2=0;
	if(IsFull())
		throw FullPQ();
	else
	{
		curr=frontPtr;
		prev=NULL;
		while((curr!=NULL)&&(msg.GetPriority()<=curr->data.GetPriority()))
		{
			curr=curr->nextPtr;			//loops until right priority spot
			num1++;
		}
		temp2=new Node;					//spot found, creates new node
		temp2->data=msg;
		temp2->nextPtr=curr;
		curr=temp2;						//stores a temporary pointer with
		temp1=curr;						//a new message at the spot
		while(num1!=0)					//begin looping to reconnect the front 
		{								//pointers
			num2=num1;
			prev=frontPtr;
			while(num2!=0)				//loop for a new previous pointer
			{							//each time the loop goes closer
				prev=prev->nextPtr;		//to the front
				num2--;
			}
			temp2=prev;
			temp2->nextPtr=temp1;
			temp1=temp2;				//assign the new previous pointers
			num1--;						//and new current pointer
		}
		if(prev==NULL)
		{
			temp2=new Node;
			temp2->data=msg;
			temp2->nextPtr=frontPtr;
			frontPtr=temp2;
		}
		else
			frontPtr=temp1;
		count++;
	}
}
void PriorityQ::Dequeue()
{
	if(IsEmpty())
		throw EmptyPQ();
	else
	{
		Node* tempPtr;
		tempPtr=frontPtr;
		frontPtr=frontPtr->nextPtr;
		delete tempPtr;
		count--;
	}
}
Message PriorityQ::Peek(int n) const
{
	int num;
	num=n;
	Node* tempPtr;
	tempPtr=frontPtr;
	if(tempPtr==NULL)
		throw InvalidPeekPQ();
	else
	{
		while(num!=0)
		{
			tempPtr=tempPtr->nextPtr;
			num--;
			if(tempPtr==NULL)
			{
				num=0;
			}
		}
		if(tempPtr==NULL)
			throw InvalidPeekPQ();
		else
			return tempPtr->data;
	}
}
bool PriorityQ::IsFull() const
{
	Node* tempPtr;
	try
	{
		tempPtr=new Node;
		delete tempPtr;
		return false;
	}
	catch(std::bad_alloc)
	{
		return true;
	}
}
bool PriorityQ::IsEmpty() const
{
	return (frontPtr==NULL);
}
int PriorityQ::Size() const
{
	return count;
}

Honestly I can't spend the time to find the exact error(s). Hopefully someone else can help you with that. But maybe a couple pointers could be helpful (no pun intended).

As great as pointers are, they are really risky. The more pointers you use the greater the chances are for a dangling reference or memory leaks, so it's best to limit their use where possible.

It may be much easier to create a priority queue using a heap structure instead. A list or some kind of a tree structure would work too, but it would be much more complicated.

I would love to do this in different method but these are the set guidelines I must follow...
I know this is long but the problematic section is between the "void PriorityQ::Enqueue(Message msg)" and "void PriorityQ::Dequeue()" anything else is irrelevant to the segmentation fault. (I hope..)

This article has been dead for over six months. Start a new discussion instead.