15 November 2011

Insert the node in the begining of the list.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
 int info;
 struct node* link;
} ;
struct node* obj;
void main()
{  void create();
void insertbeg();
void traverse();
clrscr();
create();
insertbeg();
traverse();
getch();
 }
void create()
{
 struct node*ptr,*nxt;
 char ch;
 ptr=(struct node*)malloc(sizeof(struct node));
 printf("Insert node");
 scanf("%d",&ptr->info);
 obj=ptr;
 do{
 nxt=(struct node*)malloc(sizeof(struct node));
 printf("Insert node");
 scanf("%d",&nxt->info);
 ptr->link=nxt;
 ptr=nxt;
 printf("Enter Y/N to insert more elements");
 ch=getche();
 }
 while(ch=='Y');
 ptr->link=0;
}
void insertbeg()
{       struct node*nxt;
 char c;
       // ptr=(struct node*)malloc(sizeof(struct node));
 nxt=(struct node*)malloc(sizeof(struct node));
 do{
 printf( "Do u want to insert node in the begning of the list");
 c=getche();
 }
 while(c=='Y');
 printf("insert node");
 scanf("%d",&nxt->info);
 nxt->link=obj;
       obj=nxt;
 }
void traverse()
{     struct node*ptrr;
       ptrr=obj;
      do
      {
      printf("%d",ptrr->info);
      ptrr=ptrr->link;
      } while(ptrr!=0);
  }

0 comments:

Post a Comment