Queue Data Structure
A queue is a linear Data structure which allows operations in a particular order i.e. rear or front end only. The order of queue is First In First Out (FIFO) or First Come First Serve (FCFS). A good example of the queue is any queue of consumers for a resource where the consumer that came first is served first.
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Queue implementation using an array
#include <stdio.h> #define MAX_SIZE 5 int rear=-1; int front=-1; int QUEUE[MAX_SIZE]; void Qinsert() { int data; if(rear==MAX_SIZE-1) { printf("\nQUEUE IS OVERFLOW(FULL): "); return; } else { if(rear==-1) front=0; rear=rear+1; printf("\nEnter data: "); scanf("%d",&data); QUEUE[rear]=data; return; } } void Qdelete() { int i; if(front==-1) { printf("\nQUEUE IS UNDERFLOW(EMPTY): "); return; } printf("\ndelete data:%d",QUEUE[front]); rear=rear-1; for(i=0; i<=rear; i++) QUEUE[i]=QUEUE[i+1]; if(rear==-1) front=-1; } void Qdisplay() { int i; if(rear==-1) { printf("\nQUEUE IS EMPTY"); return; } printf("\nQUEUE DATA: "); for(i=0; i<=rear; i++) printf("%d ",QUEUE[i]); getch(); return; } int main() { int option; while(1) { clrscr(); printf("\n1.FOR INSERT :"); printf("\n2.FOR DELETE :"); printf("\n3.FOR DISPLAY: "); printf("\n4.FOR EXIT...:"); scanf("%d",&option); switch(option) { case 1: Qinsert(); break; case 2: Qdelete(); getch(); break; case 3: Qdisplay(); break; case 4: return 0; default: printf("\ninvalid option: "); } } }