Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Double Ended Queue In C #1692

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions Queue/Double Ended Queue/Double_Ended_Queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <stdio.h>
#include <stdlib.h>

#define MAX 5

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

int isFull()
{
return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}

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

void insertFront(int key)
{
if (isFull())
{
printf("Overflow: Unable to insert element at the front. Deque is full.\n");
return;
}

if (front == -1)
{
front = 0;
rear = 0;
}
else if (front == 0)
{
front = MAX - 1;
}
else
{
front = front - 1;
}

deque[front] = key;
printf("Inserted %d at the front.\n", key);
}

void insertRear(int key)
{
if (isFull())
{
printf("Overflow: Unable to insert element at the rear. Deque is full.\n");
return;
}

if (rear == -1)
{
front = 0;
rear = 0;
}
else if (rear == MAX - 1)
{
rear = 0;
}
else
{
rear = rear + 1;
}

deque[rear] = key;
printf("Inserted %d at the rear.\n", key);
}

void deleteFront()
{
if (isEmpty())
{
printf("Underflow: Unable to delete element from the front. Deque is empty.\n");
return;
}

int removed = deque[front];

if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == MAX - 1)
{
front = 0;
}
else
{
front = front + 1;
}

printf("Deleted %d from the front.\n", removed);
}

void deleteRear()
{
if (isEmpty())
{
printf("Underflow: Unable to delete element from the rear. Deque is empty.\n");
return;
}

int removed = deque[rear];

if (front == rear)
{
front = -1;
rear = -1;
}
else if (rear == 0)
{
rear = MAX - 1;
}
else
{
rear = rear - 1;
}

printf("Deleted %d from the rear.\n", removed);
}

void displayDeque()
{
if (isEmpty())
{
printf("Deque is empty.\n");
return;
}

printf("Deque elements are: ");
int i = front;
while (1)
{
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}

int main()
{
insertRear(5);
displayDeque();

insertFront(15);
displayDeque();

insertRear(25);
displayDeque();

deleteFront();
displayDeque();

deleteRear();
displayDeque();

return 0;
}
56 changes: 56 additions & 0 deletions Queue/Double Ended Queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Representation

In a deque we maintain two pointers front and rear, that point to either end of the deque. The elements in a deque extend from the front end to the rear end and since it is circular, dequeue[n–1] is followed by dequeue[0].

# Insertion at Front in Deque in C

To insert an element at the front end of the deque first, check if the deque is full or not if deque is not full then follow the below approach:

Approach:

First, check the position of the front in our array.
If front < 1 , reinitialize front as the last index of the array i.e. front = N-1.
Then, add new element to array[front].

# Insertion at Rear in Deque in C

To insert an element at the rear end of the deque, follow the below approach:

Approach:

First, check if the deque is full or not.
If the deque is full , reinitialize rear with 0 (rear = 0) else increase rear by 1.
Then, add the element to array[rear].

# Deletion at Front in Deque in C

To delete an element at the front end of the deque first, follow the below approach:

Approach:

First, check if deque is empty or not.
If the deque is empty (front == -1), then we cannot perform deletion operation. In this condition, we will simply print undeflow.
If deque contains only 1 element (front = rear) , then only one deletion operation can be performed. set front = -1 and rear = -1.
Else if the front is at the last index ( front == n-1 ) , set front at starting index of deque (front = 0).
If none of the above case exists, just increment front by 1 (front = front + 1).

# Deletion at Rear in Deque in C

To delete an element at the rear end of the deque, follow the below approach:

First, check if the deque is empty or not.
If the deque is empty (front = -1), then deletion operation cannot be performed and we will print underflow.
If the deque has only 1 element( front==rear), we will set front = -1 and rear =-1.
If the rear is at the starting index of deque (rear == 0) , then set rear to last index (rear = n-1).
If none of the above case exists, just decrement rear by 1 (rear = rear-1).

# Check if Deque is Empty or Not Empty

To check if the deque is empty simply check the front if front = -1, then deque is empty else it is not empty.

# Check if Deque is Full or Not Full

To check if the deque is full simply check the below conditions:

If front == 0 and rear == n-1 , then deque is Full
If front == rear + 1 , then also deque is Full.
Loading