Saturday, August 20, 2011

How to create linked list using function.

//How to create linked list using function.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
main()
{
struct node *p;
struct node* create();
void show(struct node *);
p=create();
show(p);
}
struct node* create()
{
 struct node *q;
 int i,n;
 struct node *lst;
 struct node *p;
 q=NULL;
 lst=NULL;
 printf("Enter the elements you want in your linked list :");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  p=(struct node *)malloc(sizeof(struct node));
  printf("Enter the %d element :",i);
  scanf("%d",&(p->data));
  p->next=NULL;
  if(q==NULL)
     q=p;
  else
   lst->next=p;
  lst=p;
 }
 return q;
}
void show(struct node *start)
{
printf("Entered linked list is :\n");
while(start!=NULL)
{
printf(" %d\n",start->data);
start=start->next;
}
}

Previous Next Home
2 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More