Deletion of Any node in Circular link-List

#include<iostream.h>
#include<conio.h>
class node{
private:
int value,size;
node *last,*first,*nextnode;
public:
node()                     //constructor
{value=size=0; last=first=nextnode=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<<"\nAdress of first node: "<<first<<"  Adress first node holding: "<<first->getnext()<<endl;
cout<<"Value of First node:    "<<first->get()<<endl;
for(node *a=first->getnext();a->getnext()!=first->getnext();a=a->getnext())
 {
 cout<<"\nAdres of node:  "<<a<<"    Adress that hold: "<<a->getnext()<<endl;
 cout<<"Value of node:  "<<a->get()<<endl;
 }
}
///// find functions :)
node *find(int n)
{
for(node *a=first->getnext();a!=first;a=a->getnext())
 {
 if(a->get()==n)
 return a;
 }
}
node *find1(node *n)
{
for(node *a=first->getnext();a->getnext()!=n->getnext();a=a->getnext())
 {
 if(a->getnext()==n)
 return a;
 }
}
///// Remove Function
void remove(int n)
{
node *p1=NULL;
 if(first==NULL && last==NULL)
 {  cout<<" \n List is Empty "; return;}
 else if(first==last && n==first->get())
 {  delete first,last; first=last=NULL; cout<<"\nvalues delete";return; }
else
 {
  node *p=find(n);

  if(p==first)
   { p1=last;
    p1->setnext(first->getnext());
    first=first->getnext();
    delete p;
   }
  else if(p==last)
     { p1=find1(p);
       p1->setnext(p->getnext());
       last=p1;
       delete p;
     }
   else
   {
     p1=find1(p);
     p1->setnext(p->getnext());
      delete p;
   }
 }
}
};
 void main()
 {
 clrscr();
 int v=0;
 node obj;
 obj.add(11);
 obj.add(12);
 obj.add(13);
 obj.add(14);
 obj.add(15);
 obj.add(16);
obj.display();
while(getch()==13)
 {
 cout<<"\n\nEnter the value of node you want to delete: ";
 cin>>v;
 clrscr();
 obj.remove(v);
 obj.display();
 cout<<"\n\n\n\n\t Delete other  ::Enter    ||   Exit::Any Key ";

 }
}

No comments

Powered by Blogger.