SIMPLE QUEUE

#include<stdio.h>
#include<conio.h>
#define max 5
int queue[max],front=-1,rear=-1,item;

void enqueue(int x)
{
     if(rear==max-1)
     {
           printf("\n\tQueue Is Overflow");
     }
     else
     {
           if(front==-1)
          {
                front=0;
                rear=0;
           }
           else
           {
                rear++;
           }
           queue[rear]=x;
     }
}

void dequeue()
{
     if(front==-1)
     {
           printf("\n\tQueue Is Underflow");
           return;
     }
     item=queue[front];
     printf("\n\tThe DELETED Element : %d",item);
     if(front==rear)
     {
           front=rear=-1;
     }
     else
     {
           front++;
     }
}

void display()
{
     int i;
     for(i=front;i<=rear;i++)
     {
           printf("\t%d",queue[i]);
     }
}

void main()
{
     int ch;
     while(1)
     {
           clrscr();
           printf("\n\t--------------------------");
           printf("\n\t-:\tSimple Queue\t:-");
           printf("\n\t--------------------------");
           printf("\n\t\t1 : Enqueue");
           printf("\n\t\t2 : Dequeue");
           printf("\n\t\t3 : Display");
           printf("\n\t\t4 : Exit");
           printf("\n\t--------------------------");
           printf("\n\n\tEnter The Choice : ");
           scanf("%d",&ch);
           switch(ch)
           {
                case 1:
                     printf("\n\tEnter The Element : ");
                     scanf("%d",&item);
                     enqueue(item);
                     getch();
                     break;
                case 2:
                     dequeue();
                     getch();
                     break;
                case 3:
                     printf("\n");
                     display();
                     getch();
                     break;
                case 4:
                     exit(0);
                     break;
                default:
                     printf("\n\tIncorrect choice");
                     getch();
                     break;
           }
     }
}

                                                  
OUTPUT :

        --------------------------
        -:      Simple Queue    :-
        --------------------------
                1 : Enqueue
                2 : Dequeue
                3 : Display
                4 : Exit
        --------------------------

        Enter The Choice : 1

        Enter The Element : 78


        Enter The Choice : 1

        Enter The Element : 85


        Enter The Choice : 1

        Enter The Element : 96


        Enter The Choice : 1

        Enter The Element : 23


        Enter The Choice : 1

        Enter The Element : 85


        Enter The Choice : 1

        Enter The Element : 45

        Queue Is Overflow


        Enter The Choice : 3

        78      85      96      23      85


        Enter The Choice : 2

        The DELETED Element : 78


        Enter The Choice : 2

        The DELETED Element : 85


        Enter The Choice : 2

        The DELETED Element : 96


        Enter The Choice : 2

        The DELETED Element : 23


        Enter The Choice : 2

        The DELETED Element : 85


        Enter The Choice : 2

        Queue Is Underflow



        Enter The Choice : 4

No comments:

Post a Comment

Python Code Loop Program-1

 #Chapter -7 Qno - 2 num=4; while(num<10):     print("Number is %d"%num);     num=num+1; Output Number is 4 Number is 5 Number ...