Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. //CSC 415 Programming Languages
  2. //Matthew Tennyson
  3. //Nov 15, 2018
  4. //
  5. //Description:
  6. //  This file implements the LinkedQueue class's methods.
  7. //  The LinkedQueue class is an implementation of the Queue ADT using a
  8. //  linked structure as the underlying data structure.
  9.  
  10. #include "LinkedQueue.h"
  11. #include <exception>
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. //default constructor
  16. LinkedQueue::LinkedQueue()
  17. {
  18.     front = nullptr;
  19.     rear = nullptr;
  20.     count = 0;
  21. }
  22.  
  23. //copy constructor
  24. LinkedQueue::LinkedQueue(const LinkedQueue& original)
  25. {
  26.     front = nullptr;
  27.     rear = nullptr;
  28.     count = 0;
  29.  
  30.     Node* current = original.front;
  31.     while(current!=nullptr)
  32.     {
  33.         push(current->getValue());
  34.         current = current->getNext();
  35.     }
  36. }
  37.  
  38. //overloaded assignment operator (performs a deep copy)
  39. LinkedQueue& LinkedQueue::operator=(const LinkedQueue& original)
  40. {
  41.     //if self-assignment, do nothing
  42.     if(this==&original)
  43.         return *this;
  44.  
  45.     //deallocate any existing memory
  46.     this->~LinkedQueue();
  47.  
  48.     //reset properties, start from scratch
  49.     front = nullptr;
  50.     rear = nullptr;
  51.     count = 0;
  52.  
  53.     //copy the elements
  54.     Node* current = original.front;
  55.     while(current!=nullptr)
  56.     {
  57.         push(current->getValue());
  58.         current = current->getNext();
  59.     }
  60.  
  61.     return *this;
  62. }
  63.  
  64. //destructor
  65. LinkedQueue::~LinkedQueue()
  66. {
  67.     //TODO: FINISH THIS!
  68.     //This method should deallocate all of the memory allocated by this
  69.     //object in order to prevent memory leaks
  70.     Node* current = front;
  71.     while(current != nullptr)
  72.     {
  73.         current = front->getNext();
  74.         delete current;
  75.     }
  76. }
  77.  
  78. //push - adds an element to the back of the queue
  79. void LinkedQueue::push(char element)
  80. {
  81.     Node* newNode = new Node(element);
  82.  
  83.     if(count==0)
  84.         front = newNode;
  85.     else
  86.         rear->setNext(newNode);
  87.  
  88.     rear = newNode;
  89.     count++;
  90. }
  91.  
  92. //pop - removes an element from the front of the queue
  93. char LinkedQueue::pop()
  94. {
  95.     if(count==0)
  96.         throw invalid_argument("Attempt to pop from an empty queue.");
  97.  
  98.     if(count==1)
  99.         rear = nullptr;
  100.  
  101.     //TODO: FINISH THIS!
  102.     //This method must deallocate any memory allocated for the
  103.     //node that is being deleted.
  104.     char result = front->getValue();
  105.     Node* one = front;
  106.     front = front->getNext();
  107.     delete one;
  108.     count--;
  109.     return result;
  110. }
  111.  
  112. //peek - returns the element at the front of the queue
  113. char LinkedQueue::peek() const
  114. {
  115.     if(count==0)
  116.         throw invalid_argument("Attempt to peek at an empty queue.");
  117.  
  118.     return front->getValue();
  119. }
  120.  
  121. //size - returns the number of elements in the queue
  122. unsigned int LinkedQueue::size() const
  123. {
  124.     return count;
  125. }
  126.  
  127. //isEmpty - returns whether the queue is empty
  128. bool LinkedQueue::isEmpty() const
  129. {
  130.     return count==0;
  131. }
  132.  
  133. //overloaded equality operator (performs a deep comparison)
  134. bool LinkedQueue::operator==(const LinkedQueue& q) const
  135. {
  136.     if(count!=q.count)
  137.         return false;
  138.  
  139.     Node* current = front;
  140.     Node* other = q.front;
  141.  
  142.     while(current!=nullptr)
  143.     {
  144.         if(current->getValue()!=other->getValue())
  145.             return false;
  146.  
  147.         current = current->getNext();
  148.         other = other->getNext();
  149.     }
  150.  
  151.     return true;
  152. }
  153.  
  154. //overloaded insertion operator
  155. ostream& operator<<(ostream& out, const LinkedQueue& queue)
  156. {
  157.     out << "[ ";
  158.  
  159.     Node* current = queue.front;
  160.     while(current!=nullptr)
  161.     {
  162.         out << current->getValue() << " ";
  163.         current = current->getNext();
  164.     }
  165.     out << "]";
  166.     return out;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement