C# OOP: Queue

Remove item from Queue

Step 4: Deleting an item of the Queue

The Queue allows you to remove its first item only. The removing the item process can be done at the beginning of list. When the Queue contains only one element, you just assign the null values to the pfirst and plast.

If the Queue contains more than one item, You will need a temporary link(temp) to point to the first element and update the pfirst to point to its next item. Then assign null value to the temporary link.

The C#(Csharp) code to remove an item from the Queue is shown below:

//Remove an item from the Queue
 public ListNode<T> removeAtBack()
{
  ListNode<T> temp, del=null ;
  if (countitem() > 0)
  { //make sure the list is not empty.

//delete the first item
  if(countitem()==1){ //The list contains only one item
    del = pfirst;
    pfirst=null;
    plast=null;

}
  else{ //The list contains more than one item
    temp=pfirst;
    pfirst=pfirst.next;
    del = temp;
    temp=null;
 }
   Console.WriteLine("Deleted");

}

else Console.WriteLine("No item found");
return del ;
}




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.