C++ exercises and solutions: Count items in the Stack


Stack

Count items in the Stack


Step 3: Counting items of the Stack

To count the number of items in the Stack, you will need to traverse through the list by using a traversing pointer(i) to point to the first item of the list then move to its next item until the end of the list is reached. While the traversing pointer is moved to next, the variable that stores the number of items will increase by one.

The C++ code to count the number of items in Stack is shown below:

//Count the number of items in the stack
int countitem()
{
ListElem *i;
int t=0;
i=pfirst; //point to the first item then move it to the next
while(i!=NULL){
t=t+1; //increment
i=i->next;

}

return t; //return the number of item counted

}




Comments

h comment

 h

l;


2017-09-18



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.