Skip to main content
Queue

What is queue ?
A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).

program

/*queue using array
Fist-In-First-Out */
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct queue{
    int item[max], rear;
}q;
int index;
/*displaying queue elements*/
void display(){
    if(q.rear == -1){
        printf("\nQueue is empty");
    }else{
        printf("\nThe queue elements is: ");
        for(index=0;index<=q.rear;index++){
            printf("%d\t",q.item[index]);
        }
    }
}
/*inserting elements into queue*/
void queue(){
    int element;
    if(q.rear == (max-1)){
        printf("\nQueue is full");
    }else{
        printf("\nEnter element: ");
        scanf("%d",&element);
        q.item[++q.rear] = element;
        printf("\nEnter elements into queue successful");
        display();
    }
}
/*delete queue elements*/
void del(){
    if(q.rear == -1){
        printf("\nQueue is empty");
    }else{
        printf("\nDeleted queue element is: %d",q.item[0]);
        for(index=0;index<=q.rear;index++){
            q.item[index] = q.item[index+1];
        }
        q.item[q.rear--];
        printf("\ndeleted Queue element successful");
        display();
    }
}
/*main function*/
int main(){
    int ch;
    while(1){
        printf("\n1.insert queue;\n2.delete queue\n3.display;\n4.exit;\nEnter your choice: ");
        scanf("%d",&ch);
        switch(ch){
            case 1:
                queue();
                break;
            case 2:
                del();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(1);
                break;
            default:
                printf("\nWrong choice, try again...");
                break;
        }
    }
}

output




 

Comments