943,723 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 589
  • C++ RSS
Oct 24th, 2008
0

Please help Queue distructor problem

Expand Post »
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.
c++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
JustLearning is offline Offline
31 posts
since Sep 2008
Oct 24th, 2008
0

Re: Please help Queue distructor problem

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;
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 24th, 2008
0

Re: Please help Queue distructor problem

Thanks for looking at it but I figured it out finally.
heres what I did.
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
JustLearning is offline Offline
31 posts
since Sep 2008
Oct 24th, 2008
0

Re: Please help Queue distructor problem

you already have a dequeue function
why not use such a loop
C++ Syntax (Toggle Plain Text)
  1. while(!isempty())
  2. {
  3. dequeue();
  4. }
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Vector segment fault
Next Thread in C++ Forum Timeline: Need help: Running every 30 secs.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC