Write code in C for Linear Queue through Array?
Data Structure > Queue Data Structure > Queue Implementation using Array
516
Answer:
#include #include # define MAXSIZE 200 int queue[MAXSIZE]; int front=-1, rear=-1; //index pointing to the top of stack void main() { void enqueue(int); int dequeue(); int will=1,i,num,ch; while(will ==1) { printf("MAIN MENU: 1. Enqueue 2.Dequeue 3. Check Queue Full 4. Check Queue Empty "); Scanf ("%d", &ch); switch(ch) { case 1: printf("Enter the data to add... "); scanf("%d", &num); enqueue(num); break; case 2: num=dequeue(); if(num==-1) printf("Queue is empty");\ else printf("Value returned from pop function is %d ",num); break; case 3 : isFull(); break; case 4 : isEmpty(); break; default: printf ("Invalid Choice . "); } printf("Do you want to do more operations on Stack ( 1 for yes, any other key to exit) "); scanf("%d" , &will); } //end of outer while } //end of main void enqueue (int elem) { if(isfull()) { printf("QUEUE FULL"); return; } else{ if(front==-1) front=rear=0; else rear++; queue[rear]=elem; } } int dequeue() { int elem; if(isEmpty()) { return -1; } else { elem=queue[front]; if (front==rear) front=rear=-1; else front++; } return(elem); } int isFull() { if(rear==MAXSIZE-1) return 0; else return 1; } int isEmpty() { if((front==-1) && (front==rear)) return 0; else return 1; }
This Particular section is dedicated to Question & Answer only. If you want learn more about Data Structure. Then you can visit below links to get more depth on this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.