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

Practical 5 Code

The document contains a C program that implements a circular deque (double-ended queue) with functionalities to insert and delete elements from both the front and rear. It includes functions to check if the deque is full or empty, as well as to display its contents. The main function provides a menu-driven interface for user interaction with the deque operations.

Uploaded by

adityakusumkar
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)
2 views3 pages

Practical 5 Code

The document contains a C program that implements a circular deque (double-ended queue) with functionalities to insert and delete elements from both the front and rear. It includes functions to check if the deque is full or empty, as well as to display its contents. The main function provides a menu-driven interface for user interaction with the deque operations.

Uploaded by

adityakusumkar
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

//practical 5

//NAME:Aditya pawan kusumkar


//CLASS:CSE SY B
//ROLL NO.:CS2222
#include <stdio.h>
#include <stdbool.h>
#define MAX 5
int deque[MAX];
int front = -1, rear = -1;
bool isFull()
{
return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}
bool isEmpty()
{
return (front == -1);
}
void insertFront()
{
int x;
if (isFull())
{
printf("Deque is full!\n");
return;
}
printf("Enter Element: ");
scanf("%d", &x);
if (front == -1)
{
front = rear = 0;
}
else if (front == 0)
{
front = MAX - 1;
}
else
{
front--;
}
deque[front] = x;
}
void insertRear()
{
int x;
if (isFull())
{
printf("Deque is full!\n");
return;
}
printf("Enter Element: ");
scanf("%d", &x);
if (front == -1)
{
front = rear = 0;
}
else if (rear == MAX - 1)
{
rear = 0;
}
else
{
rear++;
}
deque[rear] = x;
}
void deleteFront()
{
if (isEmpty())
{
printf("Deque is empty!\n");
return;
}
printf("Deleted element from front: %d\n", deque[front]);
if (front == rear)
{
front = rear = -1;
}
else if (front == MAX - 1)
{
front = 0;
}
else
{
front++;
}
}
void deleteRear()
{
if (isEmpty())
{
printf("Deque is empty!\n");
return;
}
printf("Deleted element from rear: %d\n", deque[rear]);
if (front == rear)
{
front = rear = -1;
}
else if (rear == 0)
{
rear = MAX - 1;
}
else
{
rear--;
}
}
void display()
{
int i;
if (isEmpty())
{
printf("Deque is empty!\n");
return;
}
printf("Elements in deque are:\n");
i = front;
while (true)
{
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
int main()
{
int choice;
while (1)
{
printf("\n--- Circular Deque Menu ---\n");
printf("1. Insert Rear\n");
printf("2. Delete Rear\n");
printf("3. Insert Front\n");
printf("4. Delete Front\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insertRear();
break;
case 2:
deleteRear();
break;
case 3:
insertFront();
break;
case 4:
deleteFront();
break;
case 5:
display();
break;
case 6:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
}
}
}

You might also like