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

Queue (Linear)

The document contains a C program that implements a queue data structure using an array. It defines functions to check if the queue is empty or full, add elements to the queue, delete elements from the queue, and traverse the queue to display its elements. The main function demonstrates adding and deleting elements from the queue.

Uploaded by

Anjali Shakya
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)
5 views2 pages

Queue (Linear)

The document contains a C program that implements a queue data structure using an array. It defines functions to check if the queue is empty or full, add elements to the queue, delete elements from the queue, and traverse the queue to display its elements. The main function demonstrates adding and deleting elements from the queue.

Uploaded by

Anjali Shakya
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

#include<stdio.

h>
#define MAXSIZE 1000
struct QUEUE{
int elements[MAXSIZE];
int front;
int rear;
};
enum bool{FALSE, TRUE};

enum bool IS_empty(struct QUEUE *Q){


return Q->front == 1;
}
enum bool IS_full(struct QUEUE *Q){
return Q->rear == MAXSIZE-1;
}
void ADD_Q(struct QUEUE *Q, int e){
if(IS_full(Q)){
printf("Queue s full.\n");
}
else{
if(IS_empty(Q)){
Q->front=0;
}
Q-> rear++;
Q-> elements[Q->rear]=e;
}
}
int DELETE_Q(struct QUEUE *Q){
int item;
if(IS_empty(Q)){
printf("Queue is empty.\n");
return -1;
}
else{
item = Q-> elements[Q->front];
if(Q->front == Q->rear){
Q->front=Q->rear= -1;
}
else{
Q->front++;
}
return item;
}
}
void TRAVERSE(struct QUEUE *Q){
if(IS_empty(Q)){
printf("Queue is empty.\n");
}
else{
printf("Queue elements:");
for(int i=Q->front; i<=Q->rear; i++){
printf("%d", Q->elements[i]0;
}
printf("\n");
}
}
int main(){
struct QUEUE Q;
Q.front=Q.rear= -1;
ADD_Q(&Q, 10);
ADD_Q(&Q, 20);
ADD_Q(&Q, 30);
printf("After addng elements:\n");
TRAVERSE(&Q);
int deleted = DELETE_Q(&Q);
if(deleted != -1){
printf("Deleted element:%d\n", deleted);
}
printf("after deleting an element:\n");
TRAVERSE(&Q);
return 0;
}

You might also like