Data Structure (2130702)

BE | Semester-3   Winter-2017 | 11/14/2017

Q2) (c)

Given a linked list whose typical node consists of an INFO and LINK field. Formulate an algorithm which will count the number of nodes in the list.

Function: COUNT_NODES(FIRST)

  • This function counts number of nodes of the linked list and returns COUNT.
  • FIRST is a pointer to the first element of a Singly linked linear list.
  • Typical node contains INFO and LINK fields.
  • SAVE is a Temporary pointer variable.
  1. [Is list Empty]
    IF FIRST=NULL
    THEN Return(COUNT)
  2. [Initialize loop for a last node to update count]
    SAVE<--FIRST
  3. [Go for end of list]
    Repeat while LINK(SAVE)!=NULL
      SAVE<--LINK(SAVE)
      COUNT<--COUNT+1
  4. [Return Count]
    Return(COUNT)