by BehindJava
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)
- Set PTR = START. [Initialize pointer PTR]
- Repeat step 3 and 4 while PTR ≠ NULL.
- Apply PROCESS to INFO[PTR].
- Set PTR = LINK [PTR]. [PTR points to next node] [End of Step 2 Loop.]
- EXIT
Searching a Linked List
List is Unsorted.
SEARCH (INFO, LINK, START, ITEM, LOC)
- Set PTR = START.
- Repeat Step 3 While PTR ≠ NULL.
- 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.] - Set LOC = NULL. [Search is Unsuccessful.]
- Exit . [Return LOC and Exit.]
List is Sorted.
SEARCH (INFO, LINK, START, ITEM, LOC)
- Set PTR = START.
- Repeat Step 3 While PTR ≠ NULL.
- 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.] - Return LOC .
- Exit.