#include<stdio.h> int MAX=0; int front=-1; int rear=-1; int queue[100]; //Empty int isEmpty() { if(rear==-1) return 1; else return 0; } //Full int isFull() { if(rear==MAX-1) return 1; else return 0; } //Add void add() { if(isFull()) { printf("queue Overflow\n"); } else { front=0; rear++; printf("Enter the value:"); scanf("%d",&queue[rear]); printf("Value added\n"); } } //delete void del() { int i; if(isEmpty()) { printf("queue Empty\n"); } else { printf("Value removed:%d\n",queue[front]); for(i=0;i<=rear;i++) { queue[i]=queue[i+1]; } rear--; } } //Display void display() { int i; if(isEmpty()) { printf("queue Empty\n"); } else { printf("\nQueue:\n"); for(i=front;i<=rear;i++) { printf("%d\n",queue[i]); } } } //Front Rear void display_front_rear() { if(isEmpty()) { printf("Queue Empty\n"); } else { printf("Front Element:%d\n",queue[front]); printf("Rear Element:%d\n",queue[rear]); } } //Main int main() { char ch,choice; printf("Enter the max length of the queue:"); scanf("%d",&MAX); fflush(stdin); printf("MENU:\nA)ADD\nB)REMOVE\nC)DISPLAY\nD)FRONT AND REAR ELEMENT\nE)EXIT\n"); scanf("%c",&choice); fflush(stdin); while(choice!='E') { switch(choice) { case 'A': add(); break; case 'B': del(); break; case 'C': display(); break; case 'D': display_front_rear(); break; default: printf("Invalid Input"); } fflush(stdin); printf("MENU:\nA)ADD\nB)REMOVE\nC)DISPLAY\nD)FRONT AND REAR ELEMENT\nE)EXIT\n"); scanf("%c",&choice); fflush(stdin); } printf("Thank You for using our service\n"); return 0; }
Contributed By: Abhishek Banerjee, CSE,JISCSE,
Comentários