Queue | Data Structure | C Program

Queue:


Code:

#include<stdio.h>
#define MAX 5
int q[MAX];
int front=-1,rear=-1;
void in();
void de();
void di();
void main()
{
    int ch;
    while(1)
    {
        printf("Choose any option\n1.Insert\t2.Delete\t3.Display\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                in();
                break;
            case 2:
                de();
                break;
            case 3:
                di();
                break;
        }
    }
}
void in()
{
    int item;
    if(rear==MAX-1)
    {
        printf("Queue is full\n");
    }
    else
    {
        if(front==-1)
            front=0;
        rear=rear+1;
        printf("Enter value to insert\n");
        scanf("%d",&item);
        q[rear]=item;
    }
}
void de()
{
    int i;
    if(front==-1 || front>rear)
    {
        printf("Queue is empty\n");
        for(i=0;i<MAX;i++)
            printf("_\t");
    }
    else
    {
        printf("The deleted item is %d\n",q[front]);
        front=front+1;
    }
}
void di()
{
    int i;
    if(front==-1 || front>rear)
    {
        printf("Queue is Underflow!!\n");
        for(i=0;i<MAX;i++)
        {
            printf("_\t");
        }
    }
    else
    {
        for(i=0;i<front;i++)
        {
            printf("_\t");
        }
        for(i=front;i<=rear;i++)
            printf("%d\t",q[i]);
        for(i=rear+1;i<MAX;i++)
            printf("_\t");
    }
}

 Output:


 

Comments

Popular posts from this blog

Print name in a pattern | name as abbreviation | C-Program

Print Fibonacci Series using Recursion | C program :