Difference between revisions of "Remove"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
m (Spellings and stuff.. (will rewrite later))
 
Line 1: Line 1:
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
            public string Remove()
+
public string Remove()
            {
+
{
                string Item = "";
+
  string Item = "";
                if (IsEmpty())
+
  if (IsEmpty())
                {
+
  {
                    return "";
+
      return "";
                }
+
  }
                else
+
  else
                {
+
  {
                    Item = Contents[0];
+
      Item = Contents[0];
                    for (int Count = 1; Count < Rear + 1; Count++)
+
      for (int Count = 1; Count < Rear + 1; Count++)
                    {
+
      {
                        Contents[Count - 1] = Contents[Count];
+
        Contents[Count - 1] = Contents[Count];
                    }
+
      }
                    Contents[Rear] = "";
+
      Contents[Rear] = "";
                    Rear--;
+
      Rear--;
                    return Item;
+
      return Item;
                }
+
  }
            }
+
}
 
</syntaxhighlight>
 
</syntaxhighlight>
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 methood firstly checks whether the queue is empty or not, on the off codition 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.
+
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 ==
 +
* [[IsEmpty|IsEmpty()]]
 +
* Other

Latest revision as of 14:59, 14 November 2017

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