Queue implementation using array in C language







#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void search(int);
void display();
int queue[SIZE], front = -1, rear = -1;

void main()
{
    int value, choice;
    clrscr();
    while(1)
    {
        printf("\n\n***** MENU *****");
        printf("\n---------------------------");
        printf("\n 1. Insertion");
        printf("\n 2. Deletion");
        printf("\n 3. Search");
        printf("\n 4. Display");
        printf("\n 5. Exit");
        printf("\nEnter your choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                    printf("Enter the value to be insert: ");
                    scanf("%d",&value);
                    enQueue(value);
                    break;
            case 2:
                    deQueue();
                    break;
            case 3:
                    printf("Enter the value to be search");
                    scanf("%d",&value);
                    search(value);
                    break;
            case 4:
                    display();
                    break;
            case 5:
                    exit(1);
            default: 
                    printf("\nWrong selection!!! Try again!!!");
        }
    }
}
void enQueue(int value)
{
     if(rear==SIZE-1)
    {
      printf("\nQueue is Full!!! Insertion is not possible!!!");
     return;
    }
    else if(front == -1)
    {
      front=0;
     rear=0;
    }
    else
      rear++;
    queue[rear] = value;
    printf("\n Insertion success!!!");
}
void deQueue()
{
    if(front==-1)
    {
     printf("\nQueue is Empty!!! Deletion is not possible!!!");
     return;
    }
    else
    {
     printf("\n %d Deleted ", queue[front]);
     if(front == rear)
     {
        front=-1;
        rear=-1;
    }
    else
        front++;
}
void search(int n)
{
    int i,found=0;
    if(front==-1)
    {
     printf("\n Queue is empty....");
     return;
    }
    else
    {
    for(i=front;i<=rear;i++)
    {
        if(n==queue[i])
        {
            found=1;
            break;
        }
    }
    if(found==1)
        printf("\n %d found ",n);
    else
        printf("\n %d not found",n);
  }
}
void display()
{
    int i,n;
    printf("\n Queue status...:");
    if(front==-1)
    {
        printf("\n Queue is empty....");
        return;
    }
    else
    {
        for(i=front;i<=rear;i++)
            printf("%d ",queue[i]);
    }
}

Post a Comment

0 Comments