Linked List
Creating and Displaying a Linked List
Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void create_displayLinkedList(int n);
int main()
{
int n;
printf("\n To create and display Linked List:\n");
printf("Number of nodes:");
scanf("%d",&n);
create_displayLinkedList(n);
return 0;
}
void create_displayLinkedList(int n)
{
struct node *head,*newnode,*temp;
head=NULL;
int count=0;
while(n!=0)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter Data:\n");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
n--;
}
printf("***************\n");
temp=head;
while(temp!=0)
{
printf("%d->",temp->data);
temp=temp->next;
count++;
}
printf("\n Count=%d",count);
}
.png)
Comments
Post a Comment