Friday, October 16, 2009

Operation 4 – Search List :

A Search list is used by several algorithms to locate data in a list. To retrieve data from a list, we need to search the list and find the data. In addition, many user applications require that lists be searched to locate data. We present an algorithm to search for a key in an unordered list.

For simplicity, we assume that all data values in the list are unique that no data value is repeated.

Algorithm – 4: Search Unordered Linked List

algorithm searchunorderedlist (val list ,

ref pLoc ,

val key )

Searches list and passes back address of node containing key.

Pre list is metadata structure to a valid list

pLoc is pointer to variable for current node

key is the target being sought

Post pLoc points to node with equal data

-or- null if key is not found

Return true if found, false if not found

1 pLoc = list.head

2 loop (pLoc not null AND key not equal pLoc->data)

1 pLoc = pLoc->link

3 end loop

Set return value

4 if (pLoc null)

1 found = false

5 else

1 if (key equal pLoc->data)

1 found = true

2 end if

6 end if

7 return found

end searchList

No comments:

Post a Comment