All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Thursday, 6 March 2014

Creation Of A Node In Singly Link List using Structure in C

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//-------------------------------------------------

void create()
{
char ch;
 do
 {
  struct node *new_node,*current;

  new_node=(struct node *)malloc(sizeof(struct node));

  printf("\n Enter the data : ");
  scanf("%d",&new_node->data);
  new_node->next=NULL;

  if(start==NULL)
  {
  start=new_node;
  current=new_node;
  }
  else
  {
  current->next=new_node;
  current=new_node;
  }

 printf("\n Do you want to creat another : ");
 ch=getche();
 }while(ch!='n');
}
//------------------------------------------------------------------

void display()
{
struct node *new_node;
 printf("The Linked List : \n");
 new_node=start;
 while(new_node!=NULL)
   {
   printf("%d->",new_node->data);
   new_node=new_node->next;
   }
  printf(" NULL");
}
//----------------------------------------------------
void main()
{
clrscr();
create();
display();
getch();
}
//----------------------------------------------------

No comments:

Post a Comment