0% found this document useful (0 votes)
27 views2 pages

Abd

The document contains a C program that implements a simple queue data structure using an array. It includes functions for initializing the queue, checking if it is full or empty, enqueuing and dequeuing elements, and a main function that allows user interaction to add or remove elements from the queue. The program terminates when the user inputs '0', which triggers the dequeuing of all elements in the queue.

Uploaded by

mosehey993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Abd

The document contains a C program that implements a simple queue data structure using an array. It includes functions for initializing the queue, checking if it is full or empty, enqueuing and dequeuing elements, and a main function that allows user interaction to add or remove elements from the queue. The program terminates when the user inputs '0', which triggers the dequeuing of all elements in the queue.

Uploaded by

mosehey993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#define max 3
struct queue{
int arr[max];
int head;
int tail;
};
int initqueue(struct queue *q){
q->head=-1;
q->tail=-1;
}
int isfull(struct queue *q){
return q->tail==max-1;
}
int isempty(struct queue *q){
return q->head==-1;
}
void enqueue(struct queue *q,int value){
if (isfull(q)){
printf("Queue is full\n");
return -1;
}
else {
if (isempty(q)){
q->head=0;
}
q->arr[++q->tail]=value ;
printf("%d has been enqueued\n",value);
}
}
void dequeue(struct queue *q){
if (isempty(q)){
printf("queue is empty\n");
return -1;
}
else {
int value = q->arr[q->head++];
printf("%d hase been dequeued\n",value);
}
if (q->head > q->tail) {
q->head = q->tail = -1;
}
}
int main()
{
struct queue q;
int n;
initqueue(&q);
while (1){
printf("Please Enter the number:");
scanf("%d",&n);
if (n==0){
while (!isempty(&q)){
dequeue(&q);
}
break;
}
enqueue(&q,n);
}
return 0;
}

You might also like