Friday, 29 June 2012

Insert Element At First Position In A Link List


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct ll
{
     int info;
     struct ll *p;
   };
struct ll *start,*ptr;
void main()
{       clrscr();
     void infirst(ll*);
     void create (ll*);
     void print(ll*);
     start=(ll*) malloc(sizeof(ll));
     ptr=start;
     create(ptr);
       cout<<"START---->";
       print(ptr);
     infirst(ptr);
     cout<<"LINK List BECOMES:";
     print(ptr);
     getch();
}
void create (struct ll *m)
{
   char ch;
   cout<<"\n ENTER THE INFO PART:";
   cin>>m->info;
   cout<<"\n DO YOU WANT ANOTHER NODE(yes-'y' or no-'n')";
   cin>>ch;
   if(ch=='n'||ch=='N')
   {
m->p=NULL;
return;
   }
   else
   {
      m->p=(ll*)malloc(sizeof(ll));
      create(m->p);
   }
}
void print(struct ll *m)
{


    if(m->p==NULL)
    {
cout<<m->info<<"--->X";
return;
     }
     else
     {
cout<<m->info<<"---->";
print(m->p);
       }
  }
  void infirst(ll *m)
  {
struct ll *new1;
new1=(ll *)malloc(sizeof(ll));
cout<<"\n ENTER THE ITEM IN THE NODE";
cin>>new1->info;
if(m->p==NULL)
{
    new1->p=NULL;
    ptr=new1;
 }
 else
 {
      new1->p=m;
      ptr=new1;
   }


       }

No comments:

Post a Comment