Guest User

Untitled

a guest
Jun 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <new>
  2. #include "apothecary.h"
  3. #include <cassert>
  4. #include <cstddef>
  5.  
  6. using namespace std;
  7.  
  8. Apothecary::Apothecary() : backPtr(NULL), frontPtr(NULL)
  9. {
  10. }
  11.  
  12. Apothecary::Apothecary(const Apothecary& Apoth)
  13. {
  14. }
  15.  
  16. Apothecary::~Apothecary()
  17. {
  18. while (!isEmpty())
  19. dequeue();
  20. assert ((backPtr == NULL) && (frontPtr == NULL));
  21. }
  22.  
  23. bool Apothecary::isEmpty() const
  24. {
  25. return backPtr == NULL;
  26. }
  27.  
  28. void Apothecary::enqueue(const potionType& newPotion) throw(QueueException)
  29. {
  30. try
  31. {
  32. StoreNode *newPtr = new StoreNode;
  33.  
  34. newPtr ->potion = newPotion
  35.  
  36. newPtr ->next = NULL;
  37.  
  38. if(isEmpty())
  39. frontPtr = newPtr;
  40. else
  41. backPtr -> next = newPtr;
  42.  
  43. backPtr = newPtr;
  44. }
  45. catch (bad_alloc e)
  46. {
  47. throw QueueException("QueueException: enqueue cannot allocate memory.");
  48. }
  49. }
  50.  
  51. void Apothecary::dequeue() throw(QueueException)
  52. {
  53. if (isEmpty())
  54. throw QueueException(
  55. "QueueException: empty queue, cannot dequeue");
  56.  
  57. else
  58. {
  59. StoreNode *tempPtr = frontPtr;
  60. if(frontPtr == backPtr)
  61. {
  62. frontPtr = NULL;
  63. backPtr = NULL;
  64. }
  65. else
  66. frontPtr = frontPtr->next;
  67. tempPtr->next = NULL;
  68. delete tempPtr;
  69. }
  70. }
  71.  
  72. void Apothecary::dequeue(potionType& storeFront)
  73. throw(QueueException)
  74. {
  75. if (isEmpty())
  76. throw QueueException( "QueueException: empty Queue, cannot dequeue");
  77. else
  78. {
  79. queueFront = frontPtr -> potion;
  80. dequeue();
  81. }
  82. }
  83.  
  84. void Apothecary::getFront(potionType& storeFront) const
  85. throw(QueueException)
  86. {
  87. if (isEmpty())
  88. throw QueueException("QueueException: empty queue, cannot getFront");
  89. else
  90. storeFront = frontPtr->potion;
  91. }
Add Comment
Please, Sign In to add comment