Step 3: Count elements of the singly linkedlist
To count all elements of the linkedlist, we will need a loop to traverse through the linkedlist. We will let a variable (i) of ListElem type to point to the pfirst then move to its next element and increase the number of item(t) one at a time by using a while loop until the end of the linkedlist(indicated by null value) is reached.
//C# code count items in the linked list
public int countitem()
int t = 0;
for (i = pfirst; i != null; i = i.next)
t = t + 1;
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: