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

[ NEW ALGORITHM] Adding Double Ended Queue #1659

Open
2 tasks done
DinkyRajpoot56 opened this issue Nov 5, 2024 · 0 comments
Open
2 tasks done

[ NEW ALGORITHM] Adding Double Ended Queue #1659

DinkyRajpoot56 opened this issue Nov 5, 2024 · 0 comments

Comments

@DinkyRajpoot56
Copy link

DinkyRajpoot56 commented Nov 5, 2024

Issue will be closed if:

  1. You mention more than one algorithm. You can create a separate issue for each algorithm once the current one is completed.
  2. You propose an algorithm that is already present or has been mentioned in a previous issue.
  3. You create a new issue without completing your previous issue.

Note: These actions will be taken seriously. Failure to follow the guidelines may result in the immediate closure of your issue.


Name:

[Double Ended Queue]

About:

The double-ended queues, called deques for short, are a generalized form of the queue. It is exactly like a queue except that it does not follow the FIFO rule (First in first out) so, the elements can be added to or removed from either the front(head) or back(tail) of the deque.

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].

Here, front denotes the first element of the deque and rear denotes the last element of the deque.

Before performing these operations, we must follow the below two steps:

Declare an array(to be used as a deque) of size N.
Set two pointers at first position i.e. front = -1 and rear = 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:

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].

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).

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).


Labels:

new algorithm, gssoc-ext, hacktoberfest, level1


Assignees:

  • Contributor in GSSoC-ext
  • Want to work on it
@DinkyRajpoot56 DinkyRajpoot56 changed the title [ Adding Double Ended Queue] [ NEW ALGORITHM] Nov 5, 2024
@DinkyRajpoot56 DinkyRajpoot56 changed the title [ NEW ALGORITHM] [ NEW ALGORITHM] Adding Double Ended Queue Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant