Linked list 2013

Write an Algorithm OR a Method to count the number of nodes in the linked list The ,ethod declaration is given below:

int count (Node prt-start)

Algorithm to count the number of nodes in a linked list

Steps

  1. Start
  2. Set a temporary pointer to the first node and counter to 0
  3. Repeat steps 4 and 5 until the pointer reaches null
  4. Increment the counter
  5. Move the temporary pointer to the next node
  6. Return the counter value
  7. End

Method to count for the number of nodes in linked list

int count (node ptr_start)

{

Node a = new Node(ptr_start);

int c=0;

while (a!=null)

{

c++;

a= a.next;

}

return c;

}

This entry was posted in Linked list. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *