Sunday 27 January 2013

Write a program to Insert and Delete an element in the end of Singular circular linked List.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void insert_element(void);
int Delete(void);
void display(void);
struct node
{
int data;
struct node *link;
}*f=NULL,*r=NULL;

void main()
{
               int choice,x;
               clrscr();
               cout<<"-----QUEUE ---"<<endl;
               cout<<"press 1. insert the element at the ending"<<endl;
               cout<<"press 2. delete the element from the ending"<<endl;
               cout<<"press 3. display queue"<<endl;
               cout<<"press 4. EXIT"<<endl;
               while(1)
               {
                               cout<<"enter your choice:-";
                               cin>>choice;
                               switch(choice)
                               {
               case 1:

                               insert_element();
                               break;
               case 2:
                               x=Delete();
                               if(x!=-1)
                               cout<<" deleted number is="<<x<<endl;
                               else
                               cout<<" list is empty";

                               break;
               case 3:
                               cout<<" QUEUE IS"<<endl;
                               display();
                               break;
               case 4:
                               exit(0);
                               break;
               default:
                               cout<<"plz enter correct choice";
                               break;
               }//end of switch case

               getch();
               }//end of while

               }//end of main

               void insert_element()
               {
               char ch;
               do
               {
                               struct node *temp;
                               temp=new node;
                               cout<<"enter data:-";    //insert a node at the end in the
                                                                                                                                                                                                           //  circular queue
                               cin>>temp->data;
                               if(r==NULL && f==NULL)
                               {
                                              temp->link=temp;
                                              r=temp;
                                              f=temp;
                               }
                               else
                               {
                                              temp->link=f;
                                              r->link=temp;

                                              r=temp;
                               }
                               cout<<"do you want to insert more node(y/n):-";
                               cin>>ch;
                      }while(ch=='y');

               }//end of function

               int Delete()
               {
                               struct node *temp;
                               int x;

                               if(f==NULL && r==NULL)
                               {
                               return(-1);
                               }

                               else if(f==r)
                               {
                               x=r->data;      
                                  
                               temp=r;
                               f=NULL;
                               r=NULL;
                               delete(temp);
                               return(x);
                               }
                               else
                               {
                               struct node *ptr1,*ptr2;
                               ptr1=f;
                               ptr2=f->link;
                               while(ptr2->link!=f)
                               {
                               ptr1=ptr2;
                               ptr2=ptr2->link;
                               }
                               x=ptr2->data;
                               temp=ptr2;
                               ptr1->link=f;
                               r=ptr1;

                               delete(temp);
                               return(x);
                               }


               }//end of function

               void display()
               {
                     struct node  *ptr;
                     ptr=f;                          //display a queue
                     while(ptr->link!=f)
                     {
                     cout<<" "<<ptr->data;
                     ptr=ptr->link;
                     }


                     cout<<" "<<ptr->data;
                     cout<<endl;
                }//end of function

No comments:

Post a Comment