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
i=pfirst; //point to the first item then move it to the next
return t; //return the number of item counted
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: