Featured
- Get link
- X
- Other Apps
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();
}
- Get link
- X
- Other Apps
Labels:
insertion in a sorted linked list
linked list as queue
linked list insertion
sorted linked list
Popular Posts
C++ program to display numbers which get reversed after multiplying by 4
- Get link
- X
- Other Apps
Comments
Post a Comment