Circular Link-List in C++

#include<iostream.h>
#include<conio.h>
class node{
private:
int value,size;
node *last,*first,*nextnode;
public:
node()                     //constructor
{ last=first=NULL; }
int get()
{ return value;  }
void set(int value)
{ this->value=value; }
node *getnext()
{ return nextnode; }
void setnext(node *nextnode)
{ this->nextnode=nextnode; }
void add(int n)
{
 node *newnode=new node();
 newnode->set(n);
 if(first==NULL && last==NULL)
 {
  newnode->setnext(NULL);
  first=newnode;
  last=newnode;
 }
  else
 {
 last->setnext(newnode);
 newnode->setnext(first);
 last=newnode;
 }
 size++;
}
void display()
{
cout<<"Adress of first node: "<<first<<"  Adress first node holding: "<<first->getnext()<<endl;
for(node *a=first->getnext();a->getnext()!=first->getnext();a=a->getnext()){
cout<<"Adres of node: "<<a<<"    Adress that hold: "<<a->getnext()<<endl;
cout<<"Value of node: "<<a->get()<<endl<<endl;
}
}
};
 void main()
 {
 clrscr();
 node obj;
 obj.add(22);
 obj.add(33);
 obj.add(44);
 obj.add(55);
obj.display();
getch();
  }

No comments

Powered by Blogger.