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

Two Stacks to Implement a Queue #1845

Merged
merged 1 commit into from
Nov 10, 2024
Merged
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
33 changes: 33 additions & 0 deletions StackQueues/TwoStackstoImplementQueue/READme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Two Stacks to Implement a Queue

This project demonstrates how to implement a queue using two stacks in the C programming language. A queue follows the First-In-First-Out (FIFO) principle, while a stack follows the Last-In-First-Out (LIFO) principle. By using two stacks, we can simulate the behavior of a queue.

## Problem Explanation

The goal is to create a queue using two stacks (`stack1` and `stack2`). The queue will support two main operations:
1. **Enqueue (push)** - Adds an element to the end of the queue.
2. **Dequeue (pop)** - Removes an element from the front of the queue.

### Solution Approach

1. **Enqueue Operation**:
- Push the new element onto `stack1`.

2. **Dequeue Operation**:
- If `stack2` is empty, transfer all elements from `stack1` to `stack2`. This reverses the order, so the oldest element is at the top of `stack2`.
- Pop the top element from `stack2`, which is the front of the queue.

## Code Implementation

The program is written in C, with a `QueueUsingStacks` structure that contains two stacks (`stack1` and `stack2`).

### Files
- `queue_using_stacks.c`: Contains the main code for the queue implementation.

## Usage

1. Clone this repository or download the `queue_using_stacks.c` file.
2. Compile the code:
```bash
gcc queue_using_stacks.c -o queue_using_stacks

Loading