Sunday 27 January 2013

Write a program to Insert and Delete an element in the beginning 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 in the begning"<<endl;
               cout<<"press 2. delete the element from the begning"<<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();
                               cout<<" deleted number is:-"<<x<<endl;

                               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 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;

                                              f=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)
                               {
                               cout<<" no element for deletion";
                               }
                               else               //delete a node from the circular queue
                               {
                               x=f->data;
                               temp=f;
                               f=f->link;
                               r->link=f;
                               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