by BehindJava

What is Traversing, Searching Linked List in Data Structures

Home » datastructures » What is Traversing, Searching Linked List in Data Structures

In this blog, we are going to learn about Linked List Operations in Data Structures.

A linked list (One-way list) is a linear collection of data elements, called nodes, where the linear order is given by means of pointers.

  • Each node is divided into two parts.
  • First part contains the information of the element.
  • Second part contains the address of the next node in the list.

Traversing a Linked List

PTR is a pointer variable which points to the node currently being processed.
LINK [PTR] points to the next node to be processed.

Algorithm (Traversing a linked list)

  1. Set PTR = START. [Initialize pointer PTR]
  2. Repeat step 3 and 4 while PTR ≠ NULL.
  3. Apply PROCESS to INFO[PTR].
  4. Set PTR = LINK [PTR]. [PTR points to next node] [End of Step 2 Loop.]
  5. EXIT

Searching a Linked List

List is Unsorted.
SEARCH (INFO, LINK, START, ITEM, LOC)

  1. Set PTR = START.
  2. Repeat Step 3 While PTR ≠ NULL.
  3. If ITEM = INFO [PTR], then: Set LOC = PTR, and EXIT.
    Else:
    Set PTR = LINK [PTR]. [PTR points to next node]
    [End of if Structure.]
    [End of step 2 Loop.]
  4. Set LOC = NULL. [Search is Unsuccessful.]
  5. Exit . [Return LOC and Exit.]

List is Sorted.
SEARCH (INFO, LINK, START, ITEM, LOC)

  1. Set PTR = START.
  2. Repeat Step 3 While PTR ≠ NULL.
  3. If ITEM > INFO [PTR], then:
    PTR = LINK [PTR]. [PTR points to next node]
    Else if ITEM = INFO [PTR], then:
    Set LOC = PTR, and EXIT. [Search is successful.]
    Else:
    Set LOC = NULL, and EXIT. [ITEM exceeds INFO[PTR]…]
    [End of if Structure.]
    [End of step 2 Loop.]
  4. Return LOC .
  5. Exit.