C++ exercises and solutions: Remove item from Queue


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 pointer(temp) to point to the first element and update the pfirst to point to its next item. Then assign NULL value to the temporary pointer.

The C++ code to remove an item from the Queue is shown below:

//Remove an item
ListElem *removeAtFront(){

if(countitem()>0){ //make sure the list is not empty.
  ListElem *temp,*del;

  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;
}

return del;

} else return NULL;

}




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.