Double Link-List in C++

#include<iostream.h>
#include<conio.h>
class node
{
public:
int value,size;
node *nextnode,*headnode,*prenode;
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 setprev(node* prenode)
{this->prenode=prenode;}
node *getprev()
{ return prenode;}
void  display()
{ cout<<"Adress of headnode      "<<headnode<<"       Headnode hold: "<<headnode->getnext()<<endl;
for(node *a=headnode->getnext();a->getnext()!=NULL;a=a->getnext())
{

cout<<"\nAddress of  node:       "<<a;
cout<<"\nAdress of next node:    "<<a->getnext()<<"    Adress of Preves node: "<<a->getprev()<<endl;
cout<<"value of node:  "<<a->value<<endl;

}
}
};
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());
newnode->setprev(currentnode);
currentnode->setnext(newnode);
lastnode=currentnode;
currentnode=newnode;
}
else
{
newnode->setnext(NULL);
newnode->setprev(headnode);
headnode->setnext(newnode);
lastnode=headnode;
currentnode=newnode;

}
size++;
}
void main()
{
clrscr();
node obj,bol;

obj.add(10);
obj.add(20);
obj.add(30);
obj.add(40);
obj.add(50);

obj.display();

getch();
}


0 comments:

Post a Comment

My Instagram