Remove

From TRCCompSci - AQA Computer Science
Revision as of 14:59, 14 November 2017 by C3ypt1c (talk | contribs) (Spellings and stuff.. (will rewrite later))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
public string Remove()
{
   string Item = "";
   if (IsEmpty())
   {
      return "";
   }
   else
   {
      Item = Contents[0];
      for (int Count = 1; Count < Rear + 1; Count++)
      {
         Contents[Count - 1] = Contents[Count];
      }
      Contents[Rear] = "";
      Rear--;
      return Item;
   }
}

This method is a quite inefficient Get method for the queue. Recall that a queue is a first-in first-out data structure, therefore a queue should always return the first value added to it, regardless of any values added later down the line. This method firstly checks whether the queue is empty or not, on the off condition it is empty the queue returns an empty string; from a programming standpoint IMO this is ludicrous, the method should instead raise an exception if this was the condition. But I digress. Next the queue stores the value it intends to return and then shifts each value in the queue down a single index; it also then replaces the rear value to String.Empty & de-increments the rear value of the queue before returning the stored return value.

Functions and Global Variables used