how to make a linked list in c++ using the concept of operator overloading so that it supports operations like node=node+5 [on hold]

How to make a linked list that has simple add node function and display function, it uses operator overloading to add a new node
node=node+5
Here operator + is overloaded

#include<iostream.h>
#include<conio.h>
class linked{
    linked *next;
    int n;
public:
    linked(){
    n=0;
    next=NULL;
    }
/*linked* operator () (int num)
    {
   linked *n=new linked;
    return n;
    }*/
 linked* operator + (linked );
//friend void show(linked *);
};

void show(linked *nu)
{
linked * t=nu;
while(t->next!=NULL)
{cout<<t->n;
t=t->next;
}
}


linked *linked:: operator + (linked l2)
      {                                                                     
      cout<<"hi";
      linked *k=this;
      linked *m=k;
      if(m==NULL)
        {
         cout<<"hi";
         m->next=&l2;

         }
      else{while(m->next!=NULL)
        m=m->next;
      m->next=&l2; }
      return k;
      }
int main()
{
linked *obj=new linked;

getch();
obj=4+obj;
//show(obj);
obj=obj+3;
//show(obj);`enter code here`
getch();
}

This is what i have done so far. Any changes so that the code works?


Source: oop

Leave a Reply