C# OOP: Stack

Count items in 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 link(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 link is moved to next, the variable that stores the number of items will increase by one.

The C#(Csharp) code to count the number of items in Stack is shown below:

//Count the number of items in the stack

public int countitem()
{
  ListNode<T> i;
  int t = 0;
  for (i = pfirst; i != null; i = i.next)
   {
     t = t + 1;
    }
  return t;
}




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.