Sunday, 2 July 2017

C++ program of linked list

In this program we will insert,delete and print data of linked list
//Program Starts

#include<iostream>
#include<conio.h>
using namespace std;
struct node{
          int data;
          node *next;
};
class linked{
          private:
                    node *ptr,*head,*tail,*cur,*prev;
                    int d,loc;
          public:
                    void create();
                    void insatbeg();
                    void insatend();
                    void insatspec();
                    void delatbeg();
                    void delatend();
                    void delatspecind();
                    void delofspecdat();
                    void show();
};

void linked::create()
{
          head=new node;
          head->data=0;
          head -> next=NULL;
          tail=head;
          for(int i=1;i<10;i++)
          {
                    ptr= new node;
                    ptr->data=i;
                    ptr->next=NULL;
                    tail->next=ptr;
                    tail=ptr;
          }
         
}

void linked::insatbeg()
{
          cout<<"Enter data at begining : ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          ptr->next=head;
          head=ptr;
}

void linked::insatend()
{
          cout<<"Enter data at end : ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          ptr->next=NULL;
          cur=head;
         
          while(cur->next!=NULL)
          {
                    cur=cur->next;
          }
          cur->next=ptr;
}

void linked::insatspec()
{
          cout<<"Enter Location : ";
          cin>>loc;
          cout<<"Enter data: ";
          cin>>d;
          ptr= new node;
          ptr->data=d;
          prev=head;
          cur=head->next;
         
          for(int i=1;i<=loc-2;i++)
          {
                    prev= cur;
                    cur=cur->next;
          }
          prev->next=ptr;
          ptr->next=cur;
}

void linked::delatbeg()
{
          cur = head;
          head=head->next;
          delete cur;
}

void linked::delatend()
{
          prev=head;
          cur=head->next;
          while(cur->next!=NULL)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=NULL;
          delete cur;
}

void linked::delatspecind()
{
          cout<<"Enter location to be deleted : ";
          cin>>loc;
         
          prev=head;
          cur=head->next;
          for(int i=1;i<=loc-2;i++)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=cur->next;
          delete cur;
}

void linked::delofspecdat()
{
          cout<<"Enter data to be deleted : ";
          cin>>d;
         
          prev=head;
          cur=head->next;
          while(cur->data!=d)
          {
                    prev=cur;
                    cur=cur->next;
          }
          prev->next=cur->next;
          delete cur;
}

void linked::show()
{
          cur=head;
          while(cur!=NULL)
          {
                    cout<<cur->data<<" -> ";
                    cur=cur->next;
          }
}


int main()
{
          linked l1;
          int c;
          l1.create();
          do{
                    cout<<"\n\nPress 1 to insert at beg\nPress 2 to insert at end\nPress 3 to insert at specific location\nPress 4 to show\nPress 5 to delete data from begining\nPress 6 to delete data from End\nPress7 to delete data from specific location\nPress 8 to delete specific data\nPress 9 to exit\n Value==== ";
                    cin>>c;
                   
                    switch(c)
                    {
                              case 1:
                                        l1.insatbeg();
                                        break;
                              case 2:
                                        l1.insatend();
                                        break;
                              case 3:
                                        l1.insatspec();
                                        break;
                              case 4:
                                        l1.show();
                                        break;
                              case 5:
                                        l1.delatbeg();
                                        break;
                              case 6:
                                        l1.delatend();
                                        break;
                              case 7:
                                        l1.delatspecind();
                                        break;
                              case 8:
                                        l1.delofspecdat();
                                        break;
                              case 9:
                                        return 1;
                    }
          }
          while(1);
         

}

OUTPUT:
Share:

0 comments:

Post a Comment