Please help Queue distructor problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Please help Queue distructor problem

 
0
  #1
Oct 24th, 2008
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.
  1. #include <new>
  2. #include <cstddef>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. template<class SomeType>
  8. TemplateQ<SomeType>::TemplateQ()
  9. {
  10. frontPtr = NULL;
  11. rearPtr = NULL;
  12. }
  13.  
  14. template<class SomeType>
  15. void TemplateQ<SomeType>::Enqueue(SomeType newData)
  16. {
  17. if (IsFull())
  18. throw FullTemplateQ();
  19. else if (frontPtr != NULL)
  20. {
  21. rearPtr->nextPtr = new QueueNode<SomeType>;
  22. rearPtr = rearPtr->nextPtr;
  23. rearPtr->data = newData;
  24. }
  25. else
  26. {
  27. rearPtr = new QueueNode<SomeType>;
  28. rearPtr->data = newData;
  29. rearPtr->nextPtr = NULL;
  30. frontPtr = rearPtr;
  31. }
  32. }
  33.  
  34. template<class SomeType>
  35. bool TemplateQ<SomeType>::IsFull()const
  36. {
  37. QueueNode<SomeType>* tempPtr;
  38.  
  39. try
  40. {
  41. tempPtr = new QueueNode<SomeType>;
  42. delete tempPtr;
  43. return false;
  44. }
  45. catch (std::bad_alloc)
  46. {
  47. return true;
  48. }
  49. }
  50.  
  51. template<class SomeType>
  52. bool TemplateQ<SomeType>::IsEmpty()const
  53. {
  54. return (frontPtr == NULL);
  55. }
  56.  
  57. template<class SomeType>
  58. int TemplateQ<SomeType>::Size() const
  59. {
  60. int numberOfChars = 1;
  61. QueueNode<SomeType>* countPtr = frontPtr;
  62.  
  63. if (IsEmpty())
  64. return 0;
  65.  
  66. while(countPtr != rearPtr)
  67. {
  68. countPtr = countPtr->nextPtr;
  69. numberOfChars ++;
  70. }
  71. return numberOfChars;
  72. }
  73.  
  74. template<class SomeType>
  75. void TemplateQ<SomeType>::ForwardPrint()const
  76. {
  77. QueueNode<SomeType>* printPtr = frontPtr;
  78. if(IsEmpty())
  79. throw EmptyTemplateQ();
  80. else
  81. {
  82. while(printPtr != rearPtr)
  83. {
  84. cout << printPtr->data << " ";
  85. printPtr = printPtr->nextPtr;
  86. }
  87. cout << printPtr->data;
  88. }
  89. }
  90.  
  91. template<class SomeType>
  92. void TemplateQ<SomeType>::ReversePrint()const
  93. {
  94. if(IsEmpty())
  95. throw EmptyTemplateQ();
  96.  
  97. PrintNext(frontPtr);
  98. }
  99.  
  100. template<class SomeType>
  101. void TemplateQ<SomeType>::PrintNext(QueueNode<SomeType>* tempPtr) const
  102. {
  103. if (tempPtr != rearPtr)
  104. PrintNext(tempPtr->nextPtr);
  105.  
  106. cout << tempPtr->data << " ";
  107. }
  108.  
  109. template<class SomeType>
  110. SomeType TemplateQ<SomeType>::Dequeue()
  111. {
  112. if(IsEmpty())
  113. throw EmptyTemplateQ();
  114. else
  115. {
  116.  
  117. SomeType removeData = frontPtr->data;
  118. QueueNode<SomeType>* dequeuePtr = frontPtr;
  119. frontPtr = frontPtr->nextPtr;
  120. delete dequeuePtr;
  121. if(frontPtr == NULL)
  122. rearPtr = NULL;
  123.  
  124. return removeData;
  125. }
  126. }
  127.  
  128.  
  129. //*********************This is the troublesome function***********
  130. template<class SomeType>
  131. TemplateQ<SomeType>::~TemplateQ()
  132. {
  133. while(frontPtr != rearPtr)
  134. {
  135. QueueNode<SomeType>* dequeuePtr = frontPtr;
  136. frontPtr = frontPtr->nextPtr;
  137. delete dequeuePtr;
  138. }
  139.  
  140. }
  141. //*********************************************************************
When I compile and run it I get a segmentation fault.

This is the file I am trying to run
  1. c
  2. s
  3. -
  4. + w
  5. + x
  6. + y
  7. r
  8. s
  9. d
  10.  
  11. c
  12. -
  13. -
  14. p
  15. + g
  16. + h
  17. + i
  18. p
  19. r
  20. s
  21. -
  22. -
  23. -
  24. -
  25. s
  26. d

And this is my output
  1. -bash-3.2$ ./program06 p06input2.txt
  2. Constructor()
  3. Size () -- 0
  4. Dequeue() -- Failed Empty Queue
  5. Enqueue('w')
  6. Enqueue('x')
  7. Enqueue('y')
  8. Reverse Print() -- y x w
  9. Size () -- 3
  10. Destructor ()
  11. Constructor()
  12. Dequeue() -- y
  13. Segmentation fault
  14. -bash-3.2$ gedit

but my code seems to work with this file
  1. c
  2. s
  3. -
  4. p
  5. + a
  6. + b
  7. + c
  8. + d
  9. + e
  10. + f
  11. p
  12. r
  13. s
  14. -
  15. -
  16. p
  17. + g
  18. + h
  19. + i
  20. p
  21. r
  22. s
  23. -
  24. -
  25. -
  26. -
  27. -
  28. -
  29. -
  30. -
  31. s
  32. d

and the output with this file is this
  1. -bash-3.2$ ./program06 p06input1.txt
  2. Constructor()
  3. Size () -- 0
  4. Dequeue() -- Failed Empty Queue
  5. Forward Print() -- Failed Empty Queue
  6. Enqueue('a')
  7. Enqueue('b')
  8. Enqueue('c')
  9. Enqueue('d')
  10. Enqueue('e')
  11. Enqueue('f')
  12. Forward Print() -- a b c d e f
  13. Reverse Print() -- f e d c b a
  14. Size () -- 6
  15. Dequeue() -- a
  16. Dequeue() -- b
  17. Forward Print() -- c d e f
  18. Enqueue('g')
  19. Enqueue('h')
  20. Enqueue('i')
  21. Forward Print() -- c d e f g h i
  22. Reverse Print() -- i h g f e d c
  23. Size () -- 7
  24. Dequeue() -- c
  25. Dequeue() -- d
  26. Dequeue() -- e
  27. Dequeue() -- f
  28. Dequeue() -- g
  29. Dequeue() -- h
  30. Dequeue() -- i
  31. Dequeue() -- Failed Empty Queue
  32. Size () -- 0
  33. Destructor ()
The program works up to the destructor code.
can someone give me a hand?
Last edited by JustLearning; Oct 24th, 2008 at 1:01 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Please help Queue distructor problem

 
0
  #2
Oct 24th, 2008
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;
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Re: Please help Queue distructor problem

 
0
  #3
Oct 24th, 2008
Thanks for looking at it but I figured it out finally.
heres what I did.
  1. while(rearPtr != frontPtr)
  2. {
  3. QueueNode<SomeType>* dequeuePtr = frontPtr;
  4. frontPtr = frontPtr->nextPtr;
  5. delete dequeuePtr;
  6. if(frontPtr == rearPtr)
  7. {
  8. rearPtr = NULL;
  9. frontPtr = NULL;
  10. }
  11. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training

Re: Please help Queue distructor problem

 
0
  #4
Oct 24th, 2008
you already have a dequeue function
why not use such a loop
  1. while(!isempty())
  2. {
  3. dequeue();
  4. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC