To delete a node from beginning.

#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 delete_from_beginning()
          {
          node *ptr;
          ptr=head;
          if(ptr==NULL)
          cout<<"There is no node present in list. List is empty.";
          else
          {
          head=ptr->next;
          ptr->next=NULL;
          cout<<"\n\nAfter deletion of node of beginning.\n\n";
disp();
          }
          }
  void disp()
          {
  node *ptr;
            ptr=head;
           
  while(ptr!=NULL)
               {
cout<<ptr->info<<"\t";
                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<<"\n\nElements you entered\n\n";
obj.disp();
obj.delete_from_beginning();
getch();
    return 0;
}

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons