Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <stdio.

h>
#include <limits.h>
#include <stdbool.h>

#define int N 10;

//data structure for deque


int deque[N];
int front = -1;
int rear = -1;

//function prototype
void IRD(void);
void ORD(void);
void insertAtFront(int);
void insertAtRear(int);
int deleteFromFront(void);
int deleteFromRear(void);
void display(void);
bool isFull(void);
bool isEmpty(void);
int getFrontMostElement(void);
int getRearMostElement(void);

//main function i.e. entry point of the program


int main()
{
int option;
printf("\n ----MAIN MENU----");
printf("\n 1.Input restricted deque");
printf("\n 2.Output restricted deque");
printf("\n Enter your option : ");
scanf("%d", &option);

switch (option)
{
case 1:
IRD();
break;
case 2:
ORD();
break;
}

return 0;
}

//Input restricted deque


void IRD( )
{
int option;
int item;
do
{
printf("\n\n INPUT RESTRICTED DEQUE");
printf("\n 1.Insert at rear");
printf("\n 2.Delete from front");
printf("\n 3.Delete from rear");
printf("\n 4.Display");
printf("\n 5.Quit");
printf("\n Enter your option :");
scanf("%d", &option);
switch (option)
{
case 1:
printf("\n Enter the item to be added:") ;
scanf("%d", &item);
insertAtRear(item);
break;
case 2:
printf("The deleted item is %d", deleteFromFront());
break;
case 3:
printf("The deleted item is %d", deleteFromRear());
break;
case 4:
display();
break;
}
} while (option!=5);
}

//Output restricted deque


void ORD( )
{
int option;
int item;
do
{
printf("\n\n OUTPUT RESTRICTED DEQUE");
printf("\n 1.Insert at rear");
printf("\n 2.Insert at front");
printf("\n 3.Delete from front");
printf("\n 4.Display");
printf("\n 5.Quit");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the item to be added:") ;
scanf("%d", &item);
insertAtRear(item);
break;
case 2:
printf("\n Enter the item to be added:") ;
scanf("%d", &item);
insertAtFront(item);
break;
case 3:
printf("The deleted item is %d",deleteFromFront());
break;
case 4:
display();
break;
}
} while (option!=5);
}
// Checks whether Deque is full or not.
bool isFull()
{
return (bool) ((front == 0 && rear == N-1 ) || (front == rear+1));
}

//Checks whether Deque is empty or not.


bool isEmpty()
{
return (bool) (front == --1);
}

//insert at Rear
void insertAtRear(int item)
{
if (isFull())
{
printf("\n OVERFLOW");
return;
}

if (front == -1) // Queue is Empty Inititally


{
front = rear = 0;
}
else
{
if (rear == N-1) //right is at last position of queue
rear = 0;
else
rear = rear+1;
}
deque[rear] = item ;
}

//insert at Front
void insertAtFront(int item)
{
if(isFull())
{
printf("\n OVERFLOW");
return;
}

if (front == -1) //If queue is initially empty


{
front = rear = 0;
}
else
{
if(front == 0)
front = N - 1 ;
else
front = front - 1 ;
}
deque[front] = item;
}
//delete the item from front
int deleteFromFront()
{
int item;
if (isEmpty())
{
printf("\n UNDERFLOW");
return INT_MIN;
}

item = deque[front];
if (front == rear)
{
front = rear = -1 ;
}
else
{
if (front == N - 1)
front = 0;
else
front = front+1;
}
return item;
}

//delete the item from front


int deleteFromFront()
{
int item;
if (isEmpty())
{
printf("\n UNDERFLOW");
return INT_MIN;
}

item = deque[rear];
if (front == rear)
{
front = rear = -1;
}
else
{
if (rear == 0)
rear = N - 1;
else
rear = rear - 1;
}
return item;
}

//display the items.


void display( )
{
int f = front, r = rear;
if (isEmpty())
{
printf("\n QUEUE IS EMPTY");
return;
}
printf("\n The elements of the queue are : ");
if (f <= r)
{
while (f <= r)
{
printf("%d ", deque[front]);
f++;
}
printf("\n");
return;
}

while (f <= N - 1)
{
printf("%d ", deque[front]);
f++;
}
f = 0;
while (f <= r)
{
printf("%d ", deque[f]);
f++;
}
}

// Returns the front element of Deque


int getFrontMostElement()
{
// check whether Deque is empty or not
if (isEmpty())
{
printf("\n QUEUE IS EMPTY");
return INT_MIN;
}
return deque[front];
}

// Returns the front element of Deque


int getRearMostElement()
{
// check whether Deque is empty or not
if (isEmpty())
{
printf("\n QUEUE IS EMPTY");
return INT_MIN;
}
return deque[rear];
}

You might also like