Static Implementation of Circular Queue
CODE
๐
- #include<stdio.h>
- #define MAX 50
- typedef struct
- {
- int data[MAX];
- int front,rear;
- }QUEUE;
- void InitQ(QUEUE *pq)
- {
- pq->front = pq->rear = MAX-1;
- }
- void AddQ(QUEUE *pq, int num)
- {
- pq->rear=(pq->rear+1)%MAX;
- pq->data[pq->rear]=num;
- }
- int DelQ(QUEUE *pq)
- {
- pq->front=(pq->front+1)%MAX;
- return(pq->data[pq->front]);
- }
- int isempty(QUEUE *pq)
- {
- return(pq->front == pq->rear);
- }
- int isfull(QUEUE *pq)
- {
- return((pq->rear+1)%MAX==pq->front);
- }
- void main()
- {
- int num,choice;
- QUEUE q1;
- InitQ(&q1);
- printf(">>Static Implementation of Circular Queue<<\n");
- do
- {
- printf("\n1: Add \n");
- printf("2: Delete \n");
- printf("3: Exit \n");
- printf("Enter your choice: \t");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: printf("Enter Element To Be Added: \t");
- scanf("%d",&num);
- if(isfull(&q1))
- printf("Queue overflow \n");
- else
- AddQ(&q1,num);
- break;
- case 2: if(isempty(&q1))
- printf("Queue is empty \n");
- else
- printf("The Deleted Element is: %d \n",DelQ(&q1));
- break;
- case 3: printf("\t>>END<<");
- break;
- default: printf("\t>>Enter Right Choice<<");
- }
- }
- while(choice!=3);
- }
๐Execute๐
//Output
/*
>>Static Implementation of Circular Oueue<<
1: Add
2: Delete
3: Exit
Enter your choice: 1
Enter element to be added: 2
1: Add
2: Delete
3: Exit
Enter your choice: 2
The deleted element is 2
1: Add
2: Delete
3: Exit
Enter your choice: 4
>>Enter Right Choice<<
1: Add
2: Delete
3: Exit
Enter your choice: 3
>>END<<
*/
Comments
Post a Comment