Creating a Linked List Using C


Description

This program is used to create Linked List.



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


struct node
{
 int data;
 struct node* next;
};
struct node *head=NULL,*head1=NULL,*head2=NULL;

void print();
void main()
{
 int x, i,n;
 head=NULL;
 struct node* newnode,*temp;
 temp=head;
 printf("Enter no. of elements in the LIST \n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  temp=head; 
  newnode=(struct node*)malloc(sizeof(struct node*)); 
  scanf("%d",&x);
  newnode->data=x;
  if(head==NULL)
  {
   
   head=newnode;
   newnode->next=NULL;
  }
  else
  {
      while(temp->next!=NULL)
   temp=temp->next;
   temp->next=newnode;
   newnode->next=NULL; 
  }
 }
 print();
}


void print()
{
 struct node* temp;
 temp=head;
 if(head==NULL)
  printf("list is empty !!!\n\n");
 else
 {
 
  printf("\nLIST elements are\t");
  while(temp!=NULL)
  {
   printf("%d\t",temp->data);
   temp=temp->next;
  }
 }
}


OUTPUT

List Output

Labels: