DSPS: CDLL

//============================================================================
// Name        : cdll.cpp
// Author      : Parashar
// Version     :
// Copyright   : Do not try this at home ;P
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class cdll
    {
      public:
           struct node
             {
               int data;
               struct node *next,*prev;
             }*head;

      public:
           void create();
           void display();
           void addbeg();
           void addafter();
           void del();
           cdll();
    };

cdll::cdll()
       {
        head=NULL;
       }


void cdll::create()
{
    char ans='y';
    struct node *temp,*p;
        do{
           p=new node;
           p->next=p->prev=NULL;
           cout<<"Enter data\n";
           cin>>p->data;
           if(head==NULL)
              {
               head =p;
               head->next=head->prev=head;
              }
           else
           {
               temp=head;
               while(temp->next!=head)
               {
                   temp=temp->next;
               }
               temp->next=p;
               p->prev=temp;
               p->next=head;
               head->prev=p;
           }

           cout<<"Do you want to continue if yes press Y\n";
           cin>>ans;
        }
while(ans=='y'||ans=='Y');
}

void cdll::display()
{
    node *temp;
     temp=head;
     if(head==NULL)
     {
         cout<<"list is empty";
     }
     else
     {
      while(temp->next!=head)
                 {
                 cout<<temp->data<<"<->";
                 temp=temp->next;
                 }
     cout<<temp->data<<" "<<endl;
     }
}
void cdll::addbeg()
{
    struct node *temp, *p;
    temp=head;
    p= new node;
    cout<<"Enter the value to be added at beg\n";
    cin>>p->data;
    p->next=p->prev=NULL;
    p->next=head;
    head->prev=p;
    while(temp->next!=head)
            {
                temp=temp->next;
            }
    temp->next=p;
    p->prev=temp;

    head=p;
}

void cdll::addafter()
{
    struct node *temp,*p;
    int key;
    temp=head;
    p=new node;
    p->next=p->prev=NULL;
    cout<<"enter the value for new node to add after key\n";
    cin>>p->data;
    cout<<"enter the value for key\n ";
    cin>>key;
    while(temp->data!=key)
        {
        temp=temp->next;
        }
    p->next=temp->next;
    temp->next->prev=p;
    temp->next=p;
    p->prev=temp;
}
void cdll::del()
{
    struct node *temp;
    int key;
    temp=head;
    cout<<"please enter the node to be deleted\n ";
    cin>>key;

    if(head->data==key)
    {
        while(temp->next!=head)
        {
            temp=temp->next;
        }
        head=head->next;
        head->prev=temp;
        temp->next=head;
    }
    else
    {
    while(temp->data!=key)
        {
        temp=temp->next;
        }
    temp->next->prev=temp->prev;
    temp->prev->next=temp->next;
    head->next=head;
    temp->next=temp->prev=NULL;
    delete temp;
    }
}
int main()
{
    cdll s;
    int choice;
    char ch='y';
    do
    {

     cout<<endl<<"Program for Linked list";
     cout<<endl<<"1.Create";
     cout<<endl<<"2.display";
     cout<<endl<<"3.Add at beg";
     cout<<endl<<"4.Add after";
     cout<<endl<<"5.Add at end";
     cout<<endl<<"6.Delete";
     cout<<endl<<"7.exit";
     cout<<endl<<"Enter your choice";
     cin>>choice;

     switch(choice)
      {
          case 1:  s.create();
                    break;
          case 2:  s.display();
                     break;
          case 3:  s.addbeg();
                   break;
          case 4: s.addafter();
                     break;
          case 5:  s.create();
                    break;
          case 6: s.del();
                  break;
     }
    }while(ch=='y'||ch=='Y');
    return 0;
}

Comments

Popular posts from this blog

NASM: program to find largest number from an array of 32-bit numbers(hard-coded)

Rules for drawing a FLOWCHART