Wednesday, 28 August 2013

Circular Doubly Linked List & Output

//==========================================
// Name        : circulardoublyll.cpp
// Author      : your name
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//==========================================

#include <iostream>
#include<stdlib.h>

using namespace std;

class sll
{
private:
    struct node
    {
        int data;
        struct node *prev;
        struct node *next;
    }*head;
public:
    sll();
    void create();
    void display();
    void insert_first();
    void insert_rear();
};

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

void sll::create()
{
    char ans;
    int flag=1;
    node *newnode,*temp;

    do
    {
        newnode=new node;
        cout<<endl<<"Enter the element : ";
        cin>>newnode->data;

        if(flag==1)
        {
            head=newnode;
            newnode->next=head;
            newnode->prev=head;
            flag=0;
        }
        else
        {
            temp=head;
            while(temp->next!=head)
                temp=temp->next;
            temp->next=newnode;
            newnode->prev=temp;
            newnode->next=head;
            head->prev=newnode;
        }

        cout<<"\nDo you want to enter more nodes ?";
        cin>>ans;
    }while(ans=='y'||ans=='Y');
}

void sll::display()
{
    node *temp;
    temp=head;
    if(temp==NULL)
        cout<<"\nSorry !!, The list is empty !!"<<endl;
    else
    {   cout<<"->>";
        do
        {
            cout<<"\t"<<temp->data;
            temp=temp->next;
        }while(temp!=head);
    }
}

void sll::insert_first()
{
    node *p,*temp;
    p=new node;
    cout<<"\n\nEnter the element you want to insert : ";
    cin>>p->data;

    if(head==NULL)
    {
        head=p;
        p->next=head;
        p->prev=head;
    }

    else
    {
        temp=head;
        while(temp->next!=head)
            temp=temp->next;
        temp->next=p;
        p->next=head;
        head->prev=p;
        p->prev=temp;
        head=p;
        cout<<"\nThe node is inserted";
    }
}

void sll::insert_rear()
{
    node *p,*temp;
    p=new node;

    cout<<"\n\nEnter the element you want to insert : ";
    cin>>p->data;

    if(head==NULL)
    {
        head=p;
        p->next=head;
        p->prev=head;
        insert_first();
        void insert_rear();
    }
    else
    {
        temp=head;
        while(temp->next!=head)
            temp=temp->next;
        temp->next=p;
        p->next=head;
        p->prev=temp;
        head->prev=p;
        cout<<"\nThe node is inserted";
    }
}

int main()
{
    sll l;
    char ans;
    int ch;
    do
    {
        cout<<endl<<"program for Circular Linked List"<<endl;
        cout<<"1. Creat a circular linked list"<<endl;
        cout<<"2. Display"<<endl;
        cout<<"3. Insert more node at front"<<endl;
        cout<<"4. Insert more node at rear"<<endl;
        cout<<endl<<"Enter your choice";
        cin>>ch;

        switch(ch)
        {
        case 1:l.create();
                break;
        case 2:l.display();
                break;
        case 3:l.insert_first();
                break;
        case 4:l.insert_rear();
                break;
        default:cout<<"\t\tWrong Entry !!!!!";
                break;
        }

        cout<<endl<<"do you want to go to the main manu to continue(y/n)";
        cin>>ans;
    }while(ans=='y'||ans=='y');

return 0;
}

                               Output of Program
program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice1

Enter the element : 11

Do you want to enter more nodes ?
y

Enter the element : 22

Do you want to enter more nodes ?
y

Enter the element : 33

Do you want to enter more nodes ?
y

Enter the element : 44

Do you want to enter more nodes ?
y

Enter the element : 55

Do you want to enter more nodes ?
n

do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice2
->>    11    22    33    44    55
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice3


Enter the element you want to insert : 00

The node is inserted
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice2
->>    0    11    22    33    44    55
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice4


Enter the element you want to insert : 66

The node is inserted
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice2
->>    0    11    22    33    44    55    66
do you want to go to the main manu to continue(y/n)
n

No comments:

Post a Comment