Input restricted deque
unknown
plain_text
10 months ago
2.6 kB
157
Indexable
#include <stdio.h>
#define SIZE 5
int deque[SIZE];
int front = -1, rear = -1;
int isFull()
{
return (front == 0 && rear == SIZE - 1) || (front == rear + 1);
}
int isEmpty()
{
return (front == -1);
}
void insertRear(int value)
{
if (isFull())
{
printf("Deque is full.\n");
return;
}
if (isEmpty())
{
front = rear = 0;
}
else if (rear == SIZE - 1)
{
rear = 0; // Wrap around manually
}
else
{
rear++;
}
deque[rear] = value;
}
void deleteFront()
{
if (isEmpty())
{
printf("Deque is empty.\n");
return;
}
printf("Deleted from front: %d\n", deque[front]);
if (front == rear)
{
front = rear = -1;
}
else if (front == SIZE - 1)
{
front = 0; // Wrap around manually
}
else
{
front++;
}
}
void deleteRear()
{
if (isEmpty())
{
printf("Deque is empty.\n");
return;
}
printf("Deleted from rear: %d\n", deque[rear]);
if (front == rear)
{
front = rear = -1;
}
else if (rear == 0)
{
rear = SIZE - 1; // Wrap around manually
}
else
{
rear--;
}
}
void display()
{
if (isEmpty())
{
printf("Deque is empty.\n");
return;
}
printf("Deque contents: ");
int i = front;
while (1)
{
printf("%d ", deque[i]);
if (i == rear)
break;
if (i == SIZE - 1)
i = 0;
else
i++;
}
printf("\n");
}
int main()
{
int choice, value;
while (1)
{
printf("\n--- Input-Restricted Deque (No Modulo) ---\n");
printf("1. Insert at rear\n");
printf("2. Delete from front\n");
printf("3. Delete from rear\n");
printf("4. Display\n");
printf("0. Exit\n");
printf("Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &value);
insertRear(value);
break;
case 2:
deleteFront();
break;
case 3:
deleteRear();
break;
case 4:
display();
break;
case 0:
return 0;
default:
printf("Invalid choice.\n");
}
}
}
Editor is loading...
Leave a Comment