FOR FREE MATERIALS

Find Odd and Even List from Linked List

Back to Programming

Description

Here the program is written to separate the odd numbers and even numbers from a list. Here, the singly linked list is used to separate the odd and even numbers. A singly linked list is represented using the ‘structure’.

 

Each node of the list contains two parts -

# Data Part

# The Address Part 

 

The structure of each node:

 

 

Let the linked list be:

 

 

A pointer ‘ptr’ is taken to traverse the linked list which will first point to the ‘head’ and the traversing will be done till the ‘ptr’ becomes null.

 

 

Two more pointers will be needed to work as the head pointers of the list of odd numbers and even numbers.

 

First, the data of the pointer ‘ptr’ will be checked. As it is an even number then a new list of even numbers will be created and the value ‘12’ will be inserted there and the ‘ptrwill be moved to the next node of the list.

 

 

The data of ‘ptr’ will again be checked; as it is also an even number again it will be appended to the list of even numbers and the ‘ptrwill be moved.

 

 

Now the data of ‘ptris odd. So the data will be added to the new list of odd numbers and the ‘ptr’ will be moved to the next node.

 

 

For the last node, the data is even so the same process will be followed.  The even list and odd list will be:

 

Algorithm

INPUT:  The number of elements to create the list and the elements which are to be inserted.

OUTPUT:  
a) The list after creating it.
b) The two different lists containing even numbers and odd numbers separately.

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 even_odd()”
   	Step 4.1: Set ptr<-head1
	Step 4.2: while ptr≠NULL repeat step 4.3 to 4.5
Step 4.3: Create a new node in ‘p’(a ‘node’ type pointer) by using ‘malloc’ function.                    
Set data[p] <- data[ptr]
	               	Set next[p] <- Null
		Step 4.4:  if data[ptr] Mod 2≠0 then
				If odd=NULL then
					Set odd<-p
				else
					Set next[po]<-p
				[End of ‘if’]
		    	Set po<-p
			else
				if even=NULL
					Set even<-p
				else
					Set next[pe]<-p
				[End of ‘if’]
				Set pe<-p
		[End of step 4.4 ‘if’]
		Step 4.5: Set ptr<-next[ptr]
	[End of step 4.2 ‘while’ loop]
	Step 4.6: call the function ‘displaylist(even)’ to display the even list
	Step 4.7: call the function ‘displaylist(odd)’ to display the odd list
[End of function ‘void even_odd()’]

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

Step 6: Stop. 

ADVANTAGES

1. The main advantage of using a linked list is the dynamic allocation of memory. No wastage of memory is taken place as the memory is allocated for each element at the runtime.

 

DISADVANTAGES

1. The extra memory space is required to store the address of the next node.

 

TIME COMPLEXITY

The time complexity to separate the even numbers and odd numbers is O(n) where n is the number of nodes of the list.

 

SPACE COMPLEXITY

The space complexity of finding the even and odd numbers is O(n) where n is the number of nodes of the list.

Contributed by