yea I know I spelled destructor wrong what can I say my fingers got in the way of my typing

I have scoured the internet and tried several different ways to implement the destructor on my link list queue.
I highlighted the troulesome code like this
//*****************************************
code
//*****************************************

here is the program.

#include <new>
#include <cstddef>
#include <iostream>

using namespace std;

template<class SomeType>
TemplateQ<SomeType>::TemplateQ()
{
	frontPtr = NULL;
	rearPtr = NULL;
}

template<class SomeType>
void TemplateQ<SomeType>::Enqueue(SomeType newData)
{
	if (IsFull())
		throw FullTemplateQ();
	else if (frontPtr != NULL)
	{
		rearPtr->nextPtr = new  QueueNode<SomeType>;
		rearPtr = rearPtr->nextPtr;
		rearPtr->data = newData;
	}
	else
	{
		rearPtr = new  QueueNode<SomeType>;
		rearPtr->data = newData;
		rearPtr->nextPtr = NULL;
		frontPtr = rearPtr;
	}
}

template<class SomeType>
bool TemplateQ<SomeType>::IsFull()const
{
	QueueNode<SomeType>* tempPtr;

	try
	{
		tempPtr = new QueueNode<SomeType>;
		delete tempPtr;
		return false;
	}
	catch (std::bad_alloc)
	{
		return true;
	}
}

template<class SomeType>
bool TemplateQ<SomeType>::IsEmpty()const
{
	return (frontPtr == NULL);
}

template<class SomeType>
int TemplateQ<SomeType>::Size() const
{
	int numberOfChars = 1;
	QueueNode<SomeType>* countPtr = frontPtr;
	
	if (IsEmpty())
		return 0;
		
	while(countPtr != rearPtr)
	{
		countPtr = countPtr->nextPtr;
		numberOfChars ++;
	}
	return numberOfChars;
}

template<class SomeType>
void TemplateQ<SomeType>::ForwardPrint()const
{
	QueueNode<SomeType>* printPtr = frontPtr;
	if(IsEmpty())
		throw EmptyTemplateQ();
	else
	{
		while(printPtr != rearPtr)
		{
			cout << printPtr->data << " ";
			printPtr = printPtr->nextPtr;
		}
		cout << printPtr->data;
	}
}

template<class SomeType>
void TemplateQ<SomeType>::ReversePrint()const
{
	if(IsEmpty())
		throw EmptyTemplateQ();
		
	PrintNext(frontPtr);
}

template<class SomeType>
void TemplateQ<SomeType>::PrintNext(QueueNode<SomeType>* tempPtr) const
{
	if (tempPtr != rearPtr)
		PrintNext(tempPtr->nextPtr);
	
	cout << tempPtr->data << " ";
}

template<class SomeType>
SomeType TemplateQ<SomeType>::Dequeue()
{
	if(IsEmpty())
		throw EmptyTemplateQ();
	else
	{
		
		SomeType removeData = frontPtr->data;
		QueueNode<SomeType>* dequeuePtr = frontPtr;
		frontPtr = frontPtr->nextPtr;
		delete dequeuePtr;
		if(frontPtr == NULL)
			rearPtr = NULL;
		
		return removeData;
	}
}


//*********************This is the troublesome function***********
template<class SomeType>
TemplateQ<SomeType>::~TemplateQ()
{
	while(frontPtr != rearPtr)
	{
		QueueNode<SomeType>* dequeuePtr = frontPtr;
		frontPtr = frontPtr->nextPtr;
		delete dequeuePtr;
	}
		
}
//*********************************************************************

When I compile and run it I get a segmentation fault.

This is the file I am trying to run

c
s
-
+ w
+ x
+ y
r
s
d

c
-
-
p
+ g
+ h
+ i
p
r
s
-
-
-
-
s
d

And this is my output

-bash-3.2$ ./program06 p06input2.txt
Constructor()
Size () -- 0
Dequeue() -- Failed Empty Queue
Enqueue('w')
Enqueue('x')
Enqueue('y')
Reverse Print() -- y x w 
Size () -- 3
Destructor ()
Constructor()
Dequeue() -- y
Segmentation fault
-bash-3.2$ gedit

but my code seems to work with this file

c
s
-
p
+ a
+ b
+ c
+ d
+ e
+ f
p
r
s
-
-
p
+ g
+ h
+ i
p
r
s
-
-
-
-
-
-
-
-
s
d

and the output with this file is this

-bash-3.2$ ./program06 p06input1.txt
Constructor()
Size () -- 0
Dequeue() -- Failed Empty Queue
Forward Print() -- Failed Empty Queue
Enqueue('a')
Enqueue('b')
Enqueue('c')
Enqueue('d')
Enqueue('e')
Enqueue('f')
Forward Print() -- a b c d e f
Reverse Print() -- f e d c b a 
Size () -- 6
Dequeue() -- a
Dequeue() -- b
Forward Print() -- c d e f
Enqueue('g')
Enqueue('h')
Enqueue('i')
Forward Print() -- c d e f g h i
Reverse Print() -- i h g f e d c 
Size () -- 7
Dequeue() -- c
Dequeue() -- d
Dequeue() -- e
Dequeue() -- f
Dequeue() -- g
Dequeue() -- h
Dequeue() -- i
Dequeue() -- Failed Empty Queue
Size () -- 0
Destructor ()

The program works up to the destructor code.
can someone give me a hand?

Recommended Answers

All 3 Replies

I dont get it,

Your template destructor doesnt have anything to delete when the size of the class is 0,

So i guess you should just implement to apply to an empty stack too.

if *pointer==null;//maybe;

Thanks for looking at it but I figured it out finally.
heres what I did.

while(rearPtr != frontPtr)
	{
		QueueNode<SomeType>* dequeuePtr = frontPtr;
		frontPtr = frontPtr->nextPtr;
		delete dequeuePtr;
		if(frontPtr == rearPtr)
		{
			rearPtr = NULL;
			frontPtr = NULL;
		}
	}

you already have a dequeue function
why not use such a loop

while(!isempty())
{
      dequeue();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.