Wednesday 16 January 2013

inserting kth possition in single linked list (except first postion and last postion).....

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *link;
};
void main()
{
clrscr();
struct node *start=NULL,*temp,*ptr,*new1,*temp1;
int num,n,count=0,k,j;
temp=new node;
start=temp;
start->link=NULL;
start->data=10;
cout<<"\n how many number u want to enter";
cin>>n;
for(int i=0;i<n-1;i++)
{
cout<<"\n enter number";
cin>>num;
temp=new node;
temp->data=num;
temp->link=start;
start=temp;
}
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are="<<count;
cout<<"\n enter location where u want to insert";
cin>>k;
temp1=start;
for(j=1;j<k;j++,temp1=temp1->link)
{
ptr=temp1;
}
cout<<"enter data part";
cin>>num;
new1=new node;
new1->data=num;
new1->link=temp1;
ptr->link=new1;
count=0;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are now="<<count;
getch();
}

No comments:

Post a Comment