Add a node before given location of node

#include<iostream>
#include<conio.h>
using namespace std;

struct node
{
       int info;
       node *next;
};

class list
{
      public:
      int count=0;
      node *head;
      list()
      {head=NULL;}
   
       
          void add_end(int x)
          {
  if(head==NULL)
            { head=new node;
            head->info=x;
            head->next=NULL;
            count++;
  }
      else
      {
          node *ptr;
          ptr=head;
          while(ptr->next!=NULL)
          ptr=ptr->next;
          node *ctr;
          ctr=new node;
          ctr->info=x;
          ctr->next=NULL;
          ptr->next=ctr;
          count++;
  }
   }
          void add_at_loction(int y)
          {
          if(count<y)
          {
          cout<<"Not valid location because total number of of nodes you enterd is "<<count;
      }
  else
          {
          node *ptr;
          ptr=head;
         
          for(int i=1;i<y-1;i++)
          {
          ptr=ptr->next;
          }
node *ctr;
          ctr=new node;
          cout<<"Enter new element you want to enter ";
               cin>>ctr->info;
ctr->next=ptr->next;
          ptr->next=ctr;
          }
         
          }
  void disp()
          {
  node *ptr;
            ptr=head;
            cout<<"\n\nElements you entered\n";
  while(ptr!=NULL)
               {
cout<<endl<<"\t\t"<<ptr->info;
                ptr=ptr->next;
}
          }
             
};

int main()
{
    list obj;
    int n,a;
    cout<<"How many elements you want to enter?\nAns. ";
    cin>>a;
    for(int i=0;i<a;i++)
  {
    cout<<"Enter element : ";
    cin>>n;
    obj.add_end(n);
}
int l;
cout<<"Enter Loction at which you to enter a new element?\nAns. ";
cin>>l;

obj.add_at_loction(l);
    obj.disp();
    cout<<"\t\t\t\3\3\3 Smart Programmer On";
getch();
    return 0;
}


Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons