#include<bits/stdc++.h>
#define MAX_SIZE 100
using namespace std;
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
} Queue;
void initializeQueue(Queue* q)
{
q->front = -1;
q->rear = 0;
}
bool isEmpty(Queue* q) { return (q->front == q->rear - 1); }
bool isFull(Queue* q) { return (q->rear == MAX_SIZE); }
void insertion(Queue* q, int value)
{
if (isFull(q)) {
printf("Queue is full\n");
return;
}
q->items[q->rear] = value;
q->rear++;
}
void deletion(Queue* q)
{
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
}
q->front++;
}
int peek(Queue* q)
{
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
return q->items[q->front + 1];
}
void display(Queue* q)
{
if (isEmpty(q)) {
printf("\nQueue is empty\n");
return;
}
printf("\nCurrent Queue: ");
for (int i = q->front + 1; i < q->rear; i++) {
cout<<q->items[i]<<' ';
}
printf("\n");
}
int main()
{
Queue q;
initializeQueue(&q);
int n;
string s="\n\n1.insertion,\n2.deletion,\nchoose any: ";
while(cout<<s,cin>>n)
{
int data;
(n-1)? (deletion(&q)):(cout<<"\nData: ",cin>> data,insertion(&q, data));
display(&q);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBNQVhfU0laRSAxMDAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgc3RydWN0IHsKICAgIGludCBpdGVtc1tNQVhfU0laRV07CiAgICBpbnQgZnJvbnQ7CiAgICBpbnQgcmVhcjsKfSBRdWV1ZTsKCnZvaWQgaW5pdGlhbGl6ZVF1ZXVlKFF1ZXVlKiBxKQp7CiAgICBxLT5mcm9udCA9IC0xOwogICAgcS0+cmVhciA9IDA7Cn0KCmJvb2wgaXNFbXB0eShRdWV1ZSogcSkgeyByZXR1cm4gKHEtPmZyb250ID09IHEtPnJlYXIgLSAxKTsgfQoKYm9vbCBpc0Z1bGwoUXVldWUqIHEpIHsgcmV0dXJuIChxLT5yZWFyID09IE1BWF9TSVpF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AgICAgICAgaW50IGRhdGE7CiAgICAgICAgKG4tMSk/IChkZWxldGlvbigmcSkpOihjb3V0PDwiXG5EYXRhOiAiLGNpbj4+IGRhdGEsaW5zZXJ0aW9uKCZxLCBkYXRhKSk7CiAgICAgICAgZGlzcGxheSgmcSk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K
1.insertion,
2.deletion,
choose any:
Data:
Current Queue: 11
1.insertion,
2.deletion,
choose any:
Data:
Current Queue: 11 13
1.insertion,
2.deletion,
choose any:
Data:
Current Queue: 11 13 14
1.insertion,
2.deletion,
choose any:
Data:
Current Queue: 11 13 14 16
1.insertion,
2.deletion,
choose any:
Data:
Current Queue: 11 13 14 16 18
1.insertion,
2.deletion,
choose any:
Current Queue: 13 14 16 18
1.insertion,
2.deletion,
choose any:
Current Queue: 14 16 18
1.insertion,
2.deletion,
choose any:
Current Queue: 16 18
1.insertion,
2.deletion,
choose any: