[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

5

The document contains a C program that implements a 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 displaying the queue contents. The main function provides a menu-driven interface for user interaction with the queue operations.

Uploaded by

swetha.v
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)
4 views5 pages

5

The document contains a C program that implements a 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 displaying the queue contents. The main function provides a menu-driven interface for user interaction with the queue operations.

Uploaded by

swetha.v
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/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <limits.h>
#define MAX 100

typedef struct {
int items[MAX];
int front;
int rear;
} Queue;

void initializeQueue(Queue *q) {


q->front = -1;
q->rear = -1;
}

int isFull(Queue *q) {


return q->rear == MAX - 1;
}

int isEmpty(Queue *q) {


return q->front == -1;
}

void enqueue(Queue *q, int value) {


if (isFull(q)) {
printf("Queue is full!\n");
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
printf("Inserted %d\n", value);
}
}

int dequeue(Queue *q) {


int item;
if (isEmpty(q)) {
printf("Queue is empty!\n");
return INT_MIN;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
return item;
}
}

void display(Queue *q) {


if (isEmpty(q)) {
printf("Queue is empty!\n");
} else {
printf("Queue elements are: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->items[i]);
}
printf("\n");
}
}

int main() {
Queue q;
initializeQueue(&q);
int choice, value;

while (1) {
printf("\nQueue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeue(&q);
if (value != INT_MIN) {
printf("Removed %d\n", value);
}
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}

my code

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

typedef struct{
int items[MAX];
int front;
int rear;
}Queue;

void initializeQueue(Queue *q)


{
q->front=-1;
q->rear=-1;
}

int isFull(Queue *q)


{
q->rear=MAX-1;
}

int isEmpty(Queue *q)


{
q->front==-1;
}

void enqueue(Queue *q,int value)


{
if (isFull(q))
{
printf("queue is full\n");
}
else
{
if(q->front==-1)
q->front=0;
q->rear++;
q->items[q->rear]=value;
printf("inserted\n");
}
}

int dequeue(Queue *q)


{
int item;
if(isEmpty(q))
{
printf("queue empty\n");
}
else
{
item=q->items[q->front];
q->front++;
if(q->front > q->rear)
{
q->front = q->rear = -1;
}
return item;
}
}

void display(Queue *q,int i)


{
if(isEmpty(q))
{
printf("empty\n");
}
else
{
printf("elements are:\n");
for(i=q->front;i<=q->rear;i++)
{
printf("%d",q->items[i]);
}
}
}

int main()
{
Queue q;
initializeQueue(&q);
int choice,value;

while(1)
{
printf("1.insert to queue\n;
printf("2.delete from queue\n");
printf("3.display queue\n");
printf("4.exit")
printf("enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("enter the no to insert\n");
scanf("%d",value);
enqueue(&q,value);
break;
case 2 :
value = dequeue(&q);
printf("removed %d",value);
break;
case 3 :
display(&q);
break;
case 4 :
exit(0);
default:
printf("invalid choice");
}
}
return 0;
}

You might also like