CREATE OWN LIBRARY

Search element from a Linked list

Back to Programming

Description

Before the program, the structure of each node is to be defined. The structures of each node are described below.

 

Each node contains two parts:

# Data Part

# Address Part

 

And the node can be represented as:

 

 

Here the program is written to search an element in an existing list.

Let the existing list be:

 

 

Let the element to be searched is 18. First, before searching the list a ‘pointer’ (say ‘ptr’) is to be taken which will be pointing to the first node of the list (i.e. head).

 

 

First, the data of ‘ptrwill be compared with the searched element. As the data of ‘ptr’ (12) is not equal to the searched element ‘18’ the pointer ‘ptr’ will be moved to the next element.

 

 

The same process will be followed. Again the data of ‘ptr (15) will be compared with the searched element18’. In this case, also, it is not matched. So it will again move the pointer to the next node.

 

 

Now, the data of ‘ptr’ (18) is equal to the searched element18’. Therefore, the message will be printed as the element is found

This searching process will continue until the data is found in the list or the pointer becomes NULL which will occur if the element is not present in the list.

Algorithm

INPUT:  The number of elements to create the list and the elements which are to be inserted and the element to search.
OUTPUT:  a) The list after creating it.
	     b) Whether the searched element is present or not.
	     
PROCESS:
Step 1: Define a structure named ‘node’ which has two parts−
•	The data part of integer type.
•	The ‘next’ (points to it’s next node i.e. the address of the next node) part which is a structure(node) type pointer.

Step 2: Create a function “void createlist()”
 Step 2.1: Take the number of  elements from user in an ‘integer’ type variable ‘n’
.
 Step 2.2: For i=0 to (n-1) repeat  Step 2.3 to Step 2.7
 Step 2.3: Take the element which is to be inserted in an integer type variable  ‘x’.
 Step 2.4: Create a new node in ‘p’(a ‘node’ type pointer) by using ‘malloc’ function.                    
  Set data[p] <- x
  Set next[p] <- Null
 Step 2.5: If (head1 = Null) then
  Set head1 <- p
  [End of ‘If’ ]
 Step 2.6: Else   
  Set next[ptr] <- p
  [End of ‘Else’] 

 Step 2.7: Set ptr <- p

                [End of  Step 2.2 ‘For’ loop]
[End of  the function “void createlist( )”]   
 
Step 3: Create a function “void displaylist()”
  Step 3.1: Set p <-head1
  Step 3.2: While(p≠Null) then repeat  Step 3.3 and Step 3.4
  Step 3.3: Print  data[p]
  Step 3.4: Set p<-next[p]	
  [End of  Step 3.2 ‘While’ loop]
  [End of  the function “void displaylist( )”]

Step 4: Create a function “void search()”
  Step 4.1: Set c<-1
  Step 4.2: Read x [the element to search]
  Step 4.3: Set ptr<-head1
  Step 4.4: while ptr≠NULL then repeat step 4.5 to 4.7
  Step 4.5: if x=data[ptr] then
			Print "The element x is found at position c”
			break
		[End of ‘if’]
  Step 4.6: Set ptr<-next[ptr]
  Step 4.7: Set c<-c+1
	[End of 4.4 ‘while’ loop]
  Step 4.8: if ptr=NULL then
	Print "Element Not found"
	[End of 4.8 ‘if’]
[End of the function’search()’]

Step 5: [Function ‘main()’]
	Call the function ‘createlist()’
	Call the function ‘displaylist()’
Call the function ‘search()’

Step 6: Stop. 

Code

DISADVANTAGES:

1. Searching of elements in a singly linked list is sequential. The elements of the linked list cannot be accessed randomly or it cannot be traversed in revered order. So it takes a long time to search a long list if the element is near the end of the list.

 

2. A binary search is not possible in the case of the linked list. Only linear search is possible.

 

TIME COMPLEXITY

The time complexity of searching an element in a linked list is O(n) where n is the number of elements of the list.

 

SPACE COMPLEXITY

The space complexity of the singly linked list is O(n) where n is the number of elements of the list.