Minggu, 31 Oktober 2010

Program Antrian (queue) C++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#define maxsize 5

struct que
{
      int items[maxsize];
      int rear , front;
};

void insert(struct que *,int);
int remove(struct que *) ;
int empty (struct que *);
void makenull(struct que *);
void front(struct que *);


 /****************** insert function **********************/

void insert(struct que *q,int a)

 {

     if(q->rear==maxsize-1)

           q->rear=0;

        else

           (q->rear)++ ;

    if(q->rear==q->front)
         {

        cout<<endl<<"OVERFLOW" <<endl ;
           //exit(1);
         }

       q->items[q->rear]=a ;


 }

  /*************** remove function ********************/

int remove(struct que *q)
        {

         if(empty(q))

          {
        cout<<endl<<" UNDERFLOW"<<endl;

          }

        if(q->front==maxsize-1)

          q->front =0;

          else
          {
         (q->front)++;
          }
          return (q->items[q->front]);

 q->rear=q->front=maxsize-1;

        }


 /********************** empty function ************************/

 int empty(struct que *q)
       {
        

          if(q->front==q->rear)
          
            return (1);

        else

            return (0);

        }
/********************* front function ******************************/
//returns the 1st element in queue
void front(struct que *q)

{
    int a;

    int first_val;

  
    first_val=q->items[q->front];

    cout<<endl<<"FIRST ELEMENT IS :" <<first_val;
 }

  /******************** makenull function ***************************/

 void makenull(struct que *q)

 {
    if(empty(q))

        {

           cout<<"THE QUEUE  IS EMPTY";
        }

         else

        {

           q->rear=q->front=maxsize-1;

           cout<<endl<<"QUEUE Is EMPTY "<<endl ;
        }

 }


 /****************** main function *****************************/

void main ()

{

    int a;
    clrscr();
    struct que *q;


    for(int i=0;i<maxsize;i++)
    q->items[i]=0;

   char  choice;
   q->rear=q->front=maxsize-1;
      cout<<endl<<"******************* IMPLIMENTATION OF QUEUES******************" <<endl<<endl;

 do


    { //clrscr();


    cout<<endl<<endl<<" 1 : Insert "<<endl<<" 2 : Remove "<<endl<<" 3 : check empty : 0 shows  not empty & 1  shows  empty " <<endl<<" 4 : Makenull"<<endl<<" 5 : Front"<<endl;
    cout<<                          "enter ur choice : " ;

    choice=getche();

      switch(choice)

          {

            case '1' :

               {

                 cout<<endl<<"ENTER THE VALUE : ";
                 cin>>a;

                 insert(q,a);

                 for(int i=0; i<=maxsize-2;i++)

                 cout<<q->items[i]<<" ";

                 break;

               }

            case '2' :

               {
                  cout<<endl<<"THE VALUE IS: ";

                 cout<<remove(q)<<endl;

                 for(int i=maxsize-1;i>0;i--)

                 cout<<q->items[i]<<" ";

                 break;

               }



            case '3' :

               {

                 cout<<endl<<empty(q);

                 break;

               }

            case '4' :

               {

                 makenull(q);

                     for(int i=0;i<maxsize;i++)
                    {

                    q->items[i]='\0';

                    cout<<q->items[i]<<endl;

                     }

                     break;

               }

            case '5':

                {

                       front(q);

                       break;
                }


              default :



                 cout<<"  wrong choice  ";
                 break;

          }//end of switch

    cout<<endl<<"Do u want to exit y/n :" ;

    } //end do while

    while((choice=getche())!='y');

 }//end main

0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More