#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//To create structure sll
struct sll
{
int data;
struct sll*next;
};
typedef struct sll node;
//To define functions
node*create(node*);
void display(node*);
void count(node*);
void main()
{
node*head=NULL;
clrscr();
head=create(head);
display(head);
count(head);
getch();
}
//To create Linked List
node *create(node*head)
{
int i,n;
node*last=NULL,*temp=NULL;
printf("\nEnter no of node:");
scanf(" %d",&n);
for(i=0;i<n;i++)
{
temp=(node*)malloc(sizeof(node)); // Allocate memory dynamically
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
last=head;
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
}
}
return head;
}
//To display elements in Linked List
void display(node*head)
{
node*last=NULL;
printf("\ncontent:");
last=head;
while(last!=NULL)
{
printf("%d ",last->data);
last=last->next;
}
}
//To count total nodes in Linked List
void count(node*head)
{
node*last=NULL;
int count=0;
last=head;
while(last!=NULL)
{
count=count+1;
last=last->next;
}
printf("\nTotal node in linked list: %d",count);
}
0 Comments