C++ exercises and solutions: Remove item from Stack


Stack

Remove item from Stack


Step 4: Remove an item of the Stack

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

If the Stack 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 Stack is shown below:

//Remove an item
ListElem *Pop(){

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; //return the deleted item
}else return NULL;

}




Comments

jasrin comment

 jasrin

its all waste i need a full program from void main


2017-01-16



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.