Skip to main content

Featured

functions in Python

Insertion of a node in a sorted linked list


//This program inserts the node in the sorted linked list.
#include<iostream.h>
#include<conio.h>
#include<dos.h>
struct node
{
int info;
node *next;
};
void main ()
{
clrscr();
node *start,*save;
start=NULL;
char ans;
do{
   node *newptr=new node;
   cout<<"Enter Info for the new node\n";
   cin>>newptr->info;
   if(start==NULL)
   {
   start=newptr;
   newptr->next=NULL;
   save=start;
   }
   else
   {
   save->next=newptr;
   newptr->next=NULL;
   save=newptr;
   }
   cout<<"Wanna Insert new node ?(y/n)\n";
   cin>>ans;
  }while(ans=='y'||ans=='Y');
node *ptr;
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
cout<<ptr->info<<endl;
}
node *newptr=new node;
cout<<"Enter the Info for new node\n";
cin>>newptr->info;
for(save=ptr=start;ptr!=NULL;ptr=ptr->next)
{ if(newptr->info<save->info)
  {start=newptr;
   newptr->next=save;
   break;
  }
else if(save->info<newptr->info&&newptr->info<ptr->info)
  {save->next=newptr;
   newptr->next=ptr;
   break;
  }
else {save=ptr;}
}
if(ptr==NULL&&newptr->info>save->info)
{save->next=newptr;
 newptr->next=NULL;
}
cout<<"Now showing the Linked List after insertion\n";
delay(500);
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
cout<<ptr->info<<endl;
}
getch();
}

Comments

Popular Posts