[go: up one dir, main page]

0% found this document useful (0 votes)
13 views3 pages

Queue

This document contains a C program that implements a queue data structure using an array. It provides functions to enqueue, dequeue, display elements, peek at the front element, and check if the queue is full or empty. The program runs in a loop, allowing the user to perform various operations on the queue until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Queue

This document contains a C program that implements a queue data structure using an array. It provides functions to enqueue, dequeue, display elements, peek at the front element, and check if the queue is full or empty. The program runs in a loop, allowing the user to perform various operations on the queue until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#define MAX 50
int Queue[MAX], front=-1,rear=-1;
void enqueue();
int dequeue();
void display();
int peek();
int isFull();
int isEmpty();

void main()
{
int ch;
while(1)
{
printf("\nSelect 1 to Insert an element:\n");
printf("Select 2 to Delete an element:\n");
printf("Select 3 to display the elements in the Queue:\n");
printf("Select 4 to display element at the Front\n");
printf("Select 5 to Exit:\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
enqueue();
break;

case 2:
dequeue();
break;

case 3:
display();
break;

case 4:
peek();
break;

case 5:
return;
default:
printf("You have entered a wrong choice");
return;
}
}}

void enqueue()
{
if(isFull())
{
printf("Queue Overflow");
return;
}
if(front==-1)
front=0;
rear++;
printf("Enter a value");
scanf("%d", &Queue[rear]);
printf("Value inserted in the Queue");
}

int dequeue()
{
int element;
if(isEmpty())
{
printf("Queue Underflow");
return;
}
element=Queue[front];
front++;
printf("Deleted value is %d", element);
return 0;
}

void display()
{
int i;
if(isEmpty())
{
printf("Queue Underflow");
return;
}
printf("Elements present in the Queue are:\n");
for(i=front;i<=rear;i++)
printf("%d\n",Queue[i]);
}

int peek()
{
if(isEmpty())
{
printf("Queue Underflow");
return;
}
printf("Element at the front=%d", Queue[front]);
return 0;
}

int isFull()
{
if(rear==MAX-1)
return 1;
else
return 0;
}

int isEmpty()
{
if(front==-1||front==rear+1)
return 1;
else
return 0;
}

You might also like