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] Circular Queue Using Two Stacks #1793

Closed
2 tasks done
DinkyRajpoot56 opened this issue Nov 9, 2024 · 1 comment
Closed
2 tasks done

[NEW ALGORITHM] Circular Queue Using Two Stacks #1793

DinkyRajpoot56 opened this issue Nov 9, 2024 · 1 comment

Comments

@DinkyRajpoot56
Copy link

DinkyRajpoot56 commented Nov 9, 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:

[Circular Queue Using Two Stacks]

About:

xplanation of the Code:
Stack Operations:

push: Adds an element to the stack by incrementing the top pointer.
pop: Removes the element from the stack by decrementing the top pointer.
Queue Operations:

Enqueue: Adds an element to the circular queue by pushing it onto Stack 1.
Dequeue: Removes an element from the queue:
If Stack 2 is empty, all elements are moved from Stack 1 to Stack 2, reversing their order.
After that, we pop the top element from Stack 2 to return the dequeued element.
Display: Displays the elements of the queue, printing elements from Stack 2 (in reverse order) and Stack 1 (top to bottom).

Circular Queue Behavior: Although we are using two stacks, the behavior of enqueue and dequeue in a circular queue is simulated by managing the size and ensuring that the queue behaves like a circular structure. Once it’s full, it will start overwriting old elements.
Two Stack Simulation: The two-stack approach is a simple way to implement a queue by reversing the order of elements between two stacks. It allows us to perform both enqueue and dequeue operations efficiently, though each operation has a time complexity of O(n) when transferring elements between stacks.


Labels:

new algorithm, gssoc-ext, hacktoberfest, level1


Assignees:

  • Contributor in GSSoC-ext
  • Want to work on it
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

2 participants