Q:

Print the Alternate Nodes in a Linked List without using Recursion

0

Print the Alternate Nodes in a Linked List without using Recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

Pseudocode:

Function alternatedisplay()
    Begin
        print "Displaying the alternative items of 
                the list starting from the beginning"
        temp=head //initialize
        While(temp!=NULL)
            Begin
            print "temp->data"
            If(temp->next==NULL)	
                temp=NULL
            Else
                temp=temp->next->next; //jump to second next one
            End If
        End While
End Function

C Implementation:

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

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

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
	alternatedisp(head);
	
	return 0;
}

void alternatedisp(node *temp)
{
	printf("\nDisplaying the alternative items of the list starting from the begining : \n");
	while(temp!=NULL)
	{
		printf("%d ",temp->data);
		//check for end of list	
		if(temp->next==NULL) 
			temp=NULL;
		else
			//jump to second next skipping immediate one
			temp=temp->next->next; 
	}
}

Output

 
Enter the element in the list : 1

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

Enter the element in the list : 2

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 : 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 : 5

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 :
1 3 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Data Structure programs using C and C++ (Linked List Programs)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Implement Circular Doubly Linked List | C program... >>
<< Print the Alternate Nodes in a Linked List using R...