Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node* head=NULL;
struct node *cn()
{
struct node* n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}
void createnode() //Function to create Linked list.
{
int i,n,x;
struct node * temp,*newnode;
printf("Enter size of linked list :\n");
scanf("%d",&n);
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(head==NULL) //checking in linked list is empty
{
head=cn();
head->data=x;
head->next=NULL;
temp=head;
}
else
{
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
}
}
}
void display()
{
struct node * temp;
temp=head;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
while(temp->next!=NULL || temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next->next;
}
}
}
void main()
{
createnode();
printf("Displaying alternative nodes.\n");
display();
}
0 Comments