title: Deleting an element from an empty queue?
body: <p>I'm writing abstract data type of priority queue as a task for an university, which others are going to use. I have a function in my class dequeue, which deletes the first element in the queue and returns the data of this element. However when I try to delete an element from an empty queue, the program crashes. What should I do here ?</p>
  
  <p>Here's the code if it helps:</p>
  
  <pre><code>#ifndef PRIORITYQUEUE_H
  #define PRIORITYQUEUE_H
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  const int max_queue_items = 1000;
  
  template&lt;class T&gt;
  struct node{
      T data;
      int priority;
      node *next;
  };
  
  template&lt;class T&gt;
  class PriorityQueue
  {
      public:
          /*
              Constructor that creates an empty queue.
          */
          PriorityQueue(){
              head = NULL;
              size = 0;
          }
  
          /*
              Adds an element to the queue.
  
              Params:
              data - data of the element
              priority - priority of the element
          */
          bool is_empty(){
              if (size == 0){
                  return true;
              }
  
              return false;
          }
  
          bool is_full(){
              if (size == max_queue_items){
                  return true;
              }
  
              return false;
          }
  
          /*
              Adds an element to thq queue.
              It gets inserted before the first element with
              lower priority.
          */
          void enqueue(T data, int priority){
              node&lt;T&gt; * previous = NULL;
              node&lt;T&gt; * now = head;
  
              while (now != NULL &amp;&amp; now-&gt;priority &gt;= priority){
                  previous = now;
                  now = now-&gt;next;
              }
  
              node&lt;T&gt; * new_element = new node&lt;T&gt;;
              new_element-&gt;data = data;
              new_element-&gt;priority = priority;
              new_element-&gt;next = now;
  
              if (previous == NULL){
                  head = new_element;
              } else {
                  previous-&gt;next = new_element;
              }
  
              size++;
          }
  
          /*
              Removes the first element in the queue
          */
          T dequeue(){
              T data;
  
              if (!is_empty()){
                  node&lt;T&gt; * now = head;
                  data = now-&gt;data;
                  head = head-&gt;next;
                  delete now;
  
                  size--;
              }
  
              return data;
          }
  
          /*
              Returns the priority of the first element.
              It's always the highest priority in the queue.
          */
          int get_first_priority(){
              return head-&gt;priority;
          }
  
          /*
              Returns the data of the first element in the queue.
          */
          T get_first_value(){
              if (is_empty())
                  throw 0;
  
              return head-&gt;data;
          }
  
          /*
              Returns the number of elements in the queue.
          */
          int get_size(){
              return size;
          }
  
          /*
              Deletes the whole queue from the memory.
          */
          void flush(){
              node&lt;T&gt; * now;
  
              while (head != NULL){
                  now = head;
                  head = head-&gt;next;
                  delete now;
                  size--;
              }
          }
  
          /*
              Prints the whole queue following this format:
              data(priority)
          */
          void print(){
              node&lt;T&gt; * now = head;
  
              while (now != NULL){
                  cout &lt;&lt; now-&gt;data &lt;&lt; "(" &lt;&lt; now-&gt;priority &lt;&lt; ")" &lt;&lt; endl;
                  now = now-&gt;next;
              }
          }
  
      private:
          node&lt;T&gt; * head; // Pointer to the head of the queue
          int size; // Number of elements in the queue
  };
  
  #endif // PRIORITYQUEUE_H
  </code></pre>