Home » C programs

Print the Alternate Nodes in a Linked List using Recursion

Here, we write a recursive function to display the alternative nodes in the linked list, This is a C program that will print the alternative nodes in a linked list using recursion.
Submitted by Piyas Mukherjee, on January 29, 2019

Solution:

Input: A singly linked list whose address of the first node is stored in a pointer, say head

Output: The alternative nodes in the list

Data structure used: Singly linked list where each node contains a data element say data, and the address of the immediate next node say next, with Head holding the address of the first node.

Recursive function to print alternative nodes (Pseudocode):

Function alternate_display(temp):
    Begin
    IF(temp!=NULL){
        Print "temp->data"
        If(temp->next==NULL)
            temp=NULL;
        Else
            alternate_display(temp->next->next);
    }
    Else
        return control to the main function
    End IF-ELSE
End FUNCTION

C Implementation:

#include <stdio.h>
#include <stdlib.h>

typedef struct list //node structure
{
	int data;
	struct list *next;
}node;

//recursive function
void alternatedisp(node *temp); 

int main(){
	node *head=NULL,*temp,*temp1;
	int choice,count=0,key;

	//Taking the linked list as input
	do
	{
		temp=(node *)malloc(sizeof(node));
		if(temp!=NULL)
		{
			printf("\nEnter the element in the list : ");
			scanf("%d",&temp->data);
			temp->next=NULL;
			if(head==NULL)
			{	
				head=temp;
			}
			else
			{
				temp1=head;
				while(temp1->next!=NULL)
				{
					temp1=temp1->next;
				}
				temp1->next=temp;
			}
		}
		else
		{
			printf("\nMemory not avilable...node allocation is not possible");
		}
		printf("\nIf you wish to add m ore data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	//Now displaying the alternative nodes starting from the begining
	printf("\nDisplaying the alternative items of the list starting from the begining : \n");
	alternatedisp(head);
	printf("NULL\n");
	
	return 0;
}

//recursive function to display alternative nodes
void alternatedisp(node *temp) 
{
	if(temp!=NULL)
	{
		printf("%d->",temp->data);
		if(temp->next==NULL)	
			temp=NULL;
		else
			alternatedisp(temp->next->next);
	}
	else
		return;
}

Output

Enter the element in the list : 7

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 6

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 5

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 4 

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 3

If you wish to add m ore data on the list enter 1 : 0

Displaying the alternative items of the list starting from the begining :
7->5->3->NULL





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.