> For the complete documentation index, see [llms.txt](https://tanias-workspace.gitbook.io/tanias-little-corner/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tanias-workspace.gitbook.io/tanias-little-corner/data-structures/stack.md).

# Stack

A stack is a linear data structure that follows a specific order for operations: **Last In, First Out (LIFO)**. This means that the last element added to the stack is the first one to be removed. You can think of a stack like a stack of plates; you add (push) a plate on top, and when you need one, you take (pop) the topmost plate off.

#### Basic Operations on a Stack

1. **Push**: Add an element to the top of the stack.
2. **Pop**: Remove the top element from the stack.
3. **Peek (or Top)**: View the top element of the stack without removing it.
4. **isEmpty**: Check if the stack is empty.

<figure><img src="/files/5MolmmKwwM3vN9RJ4KIj" alt=""><figcaption></figcaption></figure>

## 1.Simple Stack Implementation in C++

<figure><img src="/files/QSw88UcPAgt1eQw5iG7q" alt=""><figcaption></figcaption></figure>

#### Explanation:

1. **`Stack()` Constructor**: Initializes the stack with `top = -1`, indicating that the stack is empty.
2. **`push(int x)`**: Adds an element to the top of the stack if it's not full. The `top` index is incremented, and the element is added at that position.
3. **`pop()`**: Removes and returns the top element from the stack if it's not empty. The `top` index is decremented.
4. **`peek()`**: Returns the top element without removing it, useful for checking the element at the top.
5. **`isEmpty()`**: Checks if the stack is empty by verifying if `top` is less than 0.

Main():

<figure><img src="/files/py0lTJYKLH2HJSAWiWjt" alt=""><figcaption></figcaption></figure>

## &#x20;2. Simple Stack Implementation in C++

This code gives a clear and simple demonstration of how to use a stack in C++ with basic operations like push, pop, checking the top element, checking if the stack is empty, and finding the size of the stack.

<figure><img src="/files/9W7x0xRKwnblgl7CPPCV" alt=""><figcaption></figcaption></figure>

### Main Function

* `stack<int> s;`: Here, a stack `s` of type `int` is created. This stack will hold integers.
* `s.push(10);`, `s.push(20);`, `s.push(30);`: These lines push the integers 10, 20, and 30 onto the stack. The last element pushed (`30`) will be at the top of the stack.
* `s.top()`: This function returns the top element of the stack without removing it. Since `30` was the last element pushed, it is the top element.
* The output will display `30` as the top element.
* `s.pop()`: This function removes the top element from the stack. After this operation, the stack no longer contains `30`; the new top element is `20`.
* The output will display `20` as the new top element.
* `s.empty()`: This function checks whether the stack is empty. If the stack is empty, it returns `true`; otherwise, it returns `false`.
* Since there are still elements (`10` and `20`) in the stack after the pop operation, the output will be "Stack is not empty."
* `s.size()`: This function returns the number of elements currently in the stack.
* After pushing three elements and popping one, two elements remain in the stack. The output will display `2` as the stack size.
* The `main` function returns `0`, indicating that the program executed successfully.
