Monday, August 26, 2013

IMPLEMENTATION OF QUEUE USING SLL


/*********************************************************
      NAME      :-  JABIR DAUD PATHAN
      PROGRAM   :-  IMPLEMENTATION OF QUEUE USING SLL
**********************************************************/

/*---------------<<  Header File  >>----------------*/

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

struct node
 {
  int data;
  struct node*next;
 };

/*-------<<  Declaration of class Queue >>--------*/

class queue
 {
  private :
                  node*front,*rear;
  public :
                  queue()   // Default constructor
                   {
                    front=rear=NULL;
                   }
                  int empty();        // [ Functions
                  void insert(int);   //   declaraton ]
                  int dqueue();
                  void display();
 };

/*-------<<  Function defination for empty Queue >>-------*/

int queue :: empty()
 {
   if(rear==NULL)
     return 1;
   return 0;
 }

/*-----<<  Function defination for insert element into Queue >>-----*/

void queue :: insert(int k)
 {
    if(rear==NULL)
      {
               rear=new node;
               rear->data=k;
               rear->next=NULL;
               front=rear;
      }
    else
     {
       rear->next=new node;
       rear=rear->next;
       rear->data=k;
       rear->next=NULL;
     }
 }

/*------<<  Function defination for delete element from Queue >>------*/

int queue :: dqueue()
 {
  node*p;
  int j;
  if(front==NULL)
      cout<<"\n\nQueue is empty.\n";
  else
   {
    p=front;
    j=front->data;
    front=front->next;
    delete p;

    if(front==NULL)
             rear=NULL;
   }
  return j;
 }

/*------<<  Function defination for display contents of Queue >>-----*/

void queue :: display()
 {
  node *p;
  p=front;
  cout<<"\n\nThe contents of Queue are :\n";
  if(rear==NULL)
      cout<<"\n\nQueue is empty.\n";
  else
  {
   cout<<"\n\n\tFront-->";
   while(p!=NULL)
    {
      cout<<" | "<<p->data;
      p=p->next;
    }
   cout<<" | <---Rear";
  }
 }

/*-------------<<  Main Function Starts  >>------------*/

void main()
 {
  queue q;
  int ch,j;
  char ans;
do
 {
  clrscr();
  cout<<"\n\t\t\t---<< OPERATIONS ON QUEUE >>---";
  cout<<"\n\n\t\t\t\t1.INSERT ELEMENT";
  cout<<"\n\t\t\t\t2.DELETE ELEMENT";
  cout<<"\n\t\t\t\t3.EXIT";
  cout<<"\n\n\t\t\t-------------------------------------------";

  cout<<"\n\n\t\t\tEnter your choice : ";
  cin>>ch;

  switch(ch)
   {
    case 1:
                   cout<<"\nEnter the element to be insert : ";
                   cin>>j;
                   q.insert(j);
                   q.display();
                   break;

    case 2:
                   if(q.empty())
                           {
                             cout<<"\nThe queue is empty";
                           }
                   else
                           {
                             cout<<"\nThe deleted element is :"<<q.dqueue();
                             q.display();
                           }
                   break;

    case 3:
                   exit(0);
                   break;
   }
   cout<<"\n\n\n\n\t\tDo you want to continue(Y/N)? :";
   ans=getche();
  }while(ans=='Y'||ans=='y');
   getch();
 }
/*-------------<<  End Of Main Function  >>---------------*/

/*-----------------<< OUTPUT SCREEN  >>-------------------*/


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------

                        Enter your choice : 1

Enter the element to be insert : 1

The contents of Queue are :

        Front--> | 1 | <---Rear


                Do you want to continue(Y/N)? :Y


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------

                        Enter your choice : 1

Enter the element to be insert : 2

The contents of Queue are :

        Front--> | 1 | 2 | <---Rear


                Do you want to continue(Y/N)? :Y


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------
                        Enter your choice : 1

Enter the element to be insert : 3

The contents of Queue are :

        Front--> | 1 | 2 | 3 | <---Rear


                Do you want to continue(Y/N)? :Y


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------

                        Enter your choice : 2

The deleted element is :1

The contents of Queue are :

        Front--> | 2 | 3 | <---Rear


                Do you want to continue(Y/N)? :Y


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------

                        Enter your choice : 2

The deleted element is :2

The contents of Queue are :

        Front--> | 3 | <---Rear


                Do you want to continue(Y/N)? :Y


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------

                        Enter your choice : 2

The deleted element is :3

The contents of Queue are :

Queue is empty.


                Do you want to continue(Y/N)? :Y


                        ---<< OPERATIONS ON QUEUE >>---

                                1.INSERT ELEMENT
                                2.DELETE ELEMENT
                                3.EXIT

                        ---------------------------------------------------

                        Enter your choice : 3


/*********************************************************************/


No comments:

Post a Comment