Insertion at any point in LINK-LIST
#include<iostream.h>
#include<conio.h>
class node
{
private:
int value,size;
node *nextnode,*headnode;
node *lastnode,*currentnode;
public:
node();
void add(int);
int get()
{
return value;
}
void set(int value)
{
this->value=value;
}
void setnext(node *nextnode)
{
this->nextnode=nextnode;
}
node *getnext()
{
return nextnode;
}
void inline display()
{
for(node *a=headnode->getnext();a->getnext()!=NULL;a=a->getnext())
{
cout<<"address of node "<<a<<" "<<a->getnext()<<endl;
cout<<a->value<<endl;
}
}
void find(int z)
{
int x;
cout<<endl<<"enter value";
cin>>x;
for(node *a=headnode->getnext();a->getnext()!=NULL;a=a->getnext())
if(a->get()==z)
currentnode=a;
node *newnode=new node();
newnode->set(x);
newnode->setnext(currentnode->getnext());
currentnode->setnext(newnode);
}
};
node::node()
{
headnode=new node();
headnode->setnext(NULL);
currentnode=NULL;
size=0;
}
void node::add(int n)
{
node *newnode=new node();
newnode->set(n);
if(currentnode!=NULL)
{
nextnode->setnext(currentnode->getnext());
currentnode->setnext(newnode);
lastnode=currentnode;
currentnode=newnode;
}
else
{
newnode->setnext(NULL);
headnode->setnext(newnode);
lastnode=headnode;
currentnode=newnode;
}
size++;
}
void main()
{
clrscr();
int q;
node obj;
obj.add(10);
obj.add(20);
obj.add(30);
obj.add(40);
obj.display();
cout<<endl<<"Enetr a value for search: ";
cin>>q;
obj.find(q);
obj.display();
getch();
}
#include<conio.h>
class node
{
private:
int value,size;
node *nextnode,*headnode;
node *lastnode,*currentnode;
public:
node();
void add(int);
int get()
{
return value;
}
void set(int value)
{
this->value=value;
}
void setnext(node *nextnode)
{
this->nextnode=nextnode;
}
node *getnext()
{
return nextnode;
}
void inline display()
{
for(node *a=headnode->getnext();a->getnext()!=NULL;a=a->getnext())
{
cout<<"address of node "<<a<<" "<<a->getnext()<<endl;
cout<<a->value<<endl;
}
}
void find(int z)
{
int x;
cout<<endl<<"enter value";
cin>>x;
for(node *a=headnode->getnext();a->getnext()!=NULL;a=a->getnext())
if(a->get()==z)
currentnode=a;
node *newnode=new node();
newnode->set(x);
newnode->setnext(currentnode->getnext());
currentnode->setnext(newnode);
}
};
node::node()
{
headnode=new node();
headnode->setnext(NULL);
currentnode=NULL;
size=0;
}
void node::add(int n)
{
node *newnode=new node();
newnode->set(n);
if(currentnode!=NULL)
{
nextnode->setnext(currentnode->getnext());
currentnode->setnext(newnode);
lastnode=currentnode;
currentnode=newnode;
}
else
{
newnode->setnext(NULL);
headnode->setnext(newnode);
lastnode=headnode;
currentnode=newnode;
}
size++;
}
void main()
{
clrscr();
int q;
node obj;
obj.add(10);
obj.add(20);
obj.add(30);
obj.add(40);
obj.display();
cout<<endl<<"Enetr a value for search: ";
cin>>q;
obj.find(q);
obj.display();
getch();
}
Post a Comment