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

Queue Program

This document contains a C program that implements a queue data structure with basic operations such as insertion at the rear, deletion from the front, displaying the queue contents, and peeking at the front item. The program uses an array to store the queue elements and provides a menu-driven interface for user interaction. It handles cases for an empty queue and full queue conditions appropriately.
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)
6 views3 pages

Queue Program

This document contains a C program that implements a queue data structure with basic operations such as insertion at the rear, deletion from the front, displaying the queue contents, and peeking at the front item. The program uses an array to store the queue elements and provides a menu-driven interface for user interaction. It handles cases for an empty queue and full queue conditions appropriately.
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

Created by: CPP Viewer

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int queue[MAX];
int front=-1,rear=-1;
void insert_rear();
int delete_front();
void display();
int peek();
void main()
{
int ch,item;
clrscr();
for(;;)
{
printf("Press 1 for Insert \n 2 for Delete \n 3 for display\n");
printf("Press 4 for Peek\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_rear();
break;
case 2 : item=delete_front();
if(item!=-1)
printf("Item deleted=%d",item);
break;
case 3 : display();
break;
case 4:item=peek();
if(item!=-1)
printf("First Value in the Queue=%d",item);
break;
default : exit(0);
}
}
getch();
}
void insert_rear()
{
int num;
printf("\n Enter the item to be Inserted");
scanf("%d",&num);
if(rear==MAX-1)
printf("Queue is full\n");
else if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
queue[rear]=num;
}
int delete_front()
{
int item_deleted;
if(front==-1 || front >rear)
{
printf("QUEUE is empty\n");
return -1;
}
else
{
item_deleted=queue[front];
front++;
if(front>rear)
{
front=rear=-1;
}
return item_deleted;
}
}
void display()
{
int i;
if(front==-1 || front >rear)
printf("QUEUE is empty\n");
else
{
printf("Content of the QUEUE is\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}
int peek()
{
if(front==-1 || front >rear)
printf("QUEUE is empty\n");
else
{
return queue[front];
}
}

You might also like