Header Ads

Header ADS

C++--insert-a-node-at-a-specific-position-in-a-linked-list 3

INSERT AT ANY SPECIFIC POSITION IN LINKLIST:

INSERT AT ANY POSITION









PROGRAM:


#include<iostream>
using namespace std;
struct node
{
int data;
node*next;
};
class linklist
{private:
node*start,*end;
public:
linklist()
{
start=end=0;
}
void insert(int d)
{
node*box=new node();
box->data=d;
box->next=0;
if(start==0)
{
start=end=box;
}
end->next=box;
end=box;
}
void insertatfront(int d)
{
node*box=new node();
box->data=d;
box->next=0;
if(start==0)
{
start=end=box;
}
box->next=start;
start=box;
}
void insertatpos(int d)
{
node*box=new node();
box->data=d;
box->next=0;
if(start==0)
{
start=end=box;
}}
void removeatfront(int r)
{
if(start==0)
{
cout<<"there is no node available to delete"<<endl;
}
node*temp=start;
start=start->next;
delete temp;
}
void print()
{
node*temp=start;
while(temp!=0)
{
cout<<"the data is"<<temp->data<<endl;
cout<<"the data of next is"<<temp->next<<endl;
temp=temp->next;
}}
};
void main()
{
linklist l;
l.insert(12);
l.insert(13);
l.insert(14);
l.insert(15);
l.insertatfront(11);
l.insertatfront(10);
l.removeatfront(10);
l.insertatpos();
l.print();
system("pause");
}


No comments

Theme images by RBFried. Powered by Blogger.