Linked List Searching and Traversing
Linked List Searching and Traversing
By
Ravi Kant Sahu
Asst. Professor
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely
Professional University, Punjab (India)
Memory Allocation
• Together with the linked list, a special list is maintained
which consists of unused memory cells.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Free Pool
• Linked list with free pool or list of Available space.
INFO LINK
START 1 0
2
2 A 6
3 E 9
AVAIL 4 10 C 7
5 8
6 B 4
7 D 3
8 1
9 F 0
10 5
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Free Pool
• It is a list of free nodes in the memory. It contains unused
memory cells and these memory cells can be used in future. It
is also known as ‘List of Available Space’ or ‘Free Storage List’
or ‘Free Pool’.
• It is used along with linked list. Whenever a new node is to be
inserted in the linked list, a free node is taken from the AVAIL
List and is inserted in the linked list. Similarly, whenever a node
is deleted from the linked list, it is inserted in the AVAIL List. So
that it can be used in future.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Free Pool
• It is a list of free nodes in the memory. It contains unused
memory cells and these memory cells can be used in future. It
is also known as ‘List of Available Space’ or ‘Free Storage List’
or ‘Free Pool’.
• It is used along with linked list. Whenever a new node is to be
inserted in the linked list, a free node is taken from the AVAIL
List and is inserted in the linked list. Similarly, whenever a node
is deleted from the linked list, it is inserted in the AVAIL List. So
that it can be used in future.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Free Pool
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Garbage Collection
• Garbage collection is a technique of collecting all the
deleted spaces or unused spaces in memory.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely
Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely
Professional University, Punjab (India)
Garbage Collection
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Overflow and Underflow
Overflow: When a new data are to be inserted into a data
structure but there is no available space i.e. the free storage
list is empty.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Overflow and Underflow
Underflow: When a data item is to be deleted from an
empty data structure.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Searching a Linked List
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Searching a Linked List (1)
List is Unsorted
SEARCH (INFO, LINK, START, ITEM, LOC)
1. Set PTR = START, LOC=NULL.
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. IF LOC = NULL, Then Print “ITEM not Found” [Search is
Unsuccessful.]
5. Exit . [Return LOC and Exit.]
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Searching a Linked List (2)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Review Questions
• Write an algorithm to find out the maximum and minimum
data element from an integer linked list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MCQ
• The situation when in a linked list AVAIL=NULL
is
• a. underflow
• b. overflow
• c. housefull
• d. saturated
• A. :
• B. )
• C. ;
• D. none of the mentioned