Hello guys
#include<stdio.h> #define MAX 5 int queue[MAX], front = -1, rear = -1; void disp() { int i; if(front==-1&&rear==-1) { printf("\n Queue is empty!!!"); } else { if(front<=rear) { printf("\n The queue elements are :"); for(i = front; i<=rear; i++) { printf("%d ",queue[i]); } } else { printf("\n The queue elements are :"); for(i =front; i<MAX; i++ ) printf("%d ",queue[i]); for(i =0; i<=rear; i++ ) printf("%d ",queue[i]); } } } void enqueue(int val) { //circulated and filled if( front == rear+1 ) printf("\n Overflow"); // filled like linear queue else if( front == 0 && rear == MAX-1 ) printf("\n Overflow"); // the whole queue is empty else if( rear == -1 && front == -1 ) { rear = 0; front = 0; queue[rear] = val; printf("\n %d successfully enqueued!!!",val); disp(); } //rear has reached to last position and front[0] is empty else if(rear == MAX-1 && front!=0) { rear = 0; queue[rear] = val; printf("\n %d successfully enqueued!!!",val); disp(); } // normal insertion else { rear++; queue[rear] = val; printf("\n %d successfully enqueued!!!",val); disp(); } } void dequeue() { int x; // queue is empty if(rear == -1 && front == -1) printf("\n Underflow"); // only one element else if(front == rear) { x = queue[front]; front = -1; rear = -1; printf("\n %d successfully dequeued",x); disp(); } // end element else if(front == MAX-1 ) { x = queue[front]; front = 0; printf("\n %d successfully dequeued",x); disp(); } // normal deletion else { x = queue[front]; front++; printf("\n %d successfully dequeued",x); disp(); } } void main() { int ch,e; do { printf("\n===MENU==="); printf("\n 1. ENQUEUE"); printf("\n 2. DEQUEUE"); printf("\n 3. DISPLAY"); printf("\n 4. EXIT"); printf("\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1 : if((front == 0 && rear == MAX-1) || (front == rear+1) ) printf("\n Element cannot be inserted \n Queue overflow"); else{ printf("\n Enter the element : "); scanf("%d",&e); enqueue(e);} break; case 2 : dequeue(); break; case 3 : disp(); break; case 4 : printf("\n Thank you!!! \n "); break; default : printf("\n Invalid Input "); break; } }while(ch!=4); } /* OUTPUT : ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Enter the element : 56 56 successfully enqueued!!! The queue elements are :56 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Enter the element : 27 27 successfully enqueued!!! The queue elements are :56 27 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Enter the element : 44 44 successfully enqueued!!! The queue elements are :56 27 44 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Enter the element : 48 48 successfully enqueued!!! The queue elements are :56 27 44 48 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Enter the element : 29 29 successfully enqueued!!! The queue elements are :56 27 44 48 29 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Element cannot be inserted Queue overflow ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 2 56 successfully dequeued The queue elements are :27 44 48 29 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 1 Enter the element : 2 2 successfully enqueued!!! The queue elements are :27 44 48 29 2 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 3 The queue elements are :27 44 48 29 2 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 2 27 successfully dequeued The queue elements are :44 48 29 2 ===MENU=== 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT Enter your choice : 2
No comments:
Post a Comment