DSPS: CDLL (final update with output)

//============================================================================
// 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"<<endl;
           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 u wanna continue,if yes-press y"<<endl;
           cin>>ans;
        }
while(ans=='y'||ans=='Y');
}

void cdll::display()
{
    node *temp;
     temp=head;
     if(head==NULL)
     {
         cout<<"list is empty"<<endl;
     }
     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 add in the beginning"<<endl;
    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 a new value to add"<<endl;
    cin>>p->data;
    cout<<"add this value after: ";
    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<<"Enter the value to be deleted"<<endl;
    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 Circular Linked list";
     cout<<endl<<"1.Create list";
     cout<<endl<<"2.display list";
     cout<<endl<<"3.Add at beginning";
     cout<<endl<<"4.Add after";
     cout<<endl<<"5.Add at end";
     cout<<endl<<"6.Delete number";
     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;
}

/*
Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice1
Enter data
10
Do u wanna continue,if yes-press y
y
Enter data
20
Do u wanna continue,if yes-press y
y
Enter data
30
Do u wanna continue,if yes-press y
y
Enter data
40
Do u wanna continue,if yes-press y
y
Enter data
50
Do u wanna continue,if yes-press y
n

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice2
10<->20<->30<->40<->50

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice3
Enter the value to add in the beginning
5

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice4
enter a new value to add
15
add this value after: 10

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice2
5<->10<->15<->20<->30<->40<->50

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice5
Enter data
55
Do u wanna continue,if yes-press y
n

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice2
5<->10<->15<->20<->30<->40<->50<->55

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice6
Enter the value to be deleted
5

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice2
10<->15<->20<->30<->40<->50<->55

Program for Circular Linked list
1.Create list
2.display list
3.Add at beginning
4.Add after
5.Add at end
6.Delete number
7.exit
Enter your choice
*/

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