## Stack

**What is Stack? **

* In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and. pop, which removes the most recently added element that was not yet removed. *

-Wikipedia

Consider a stack of chairs as shown in the picture below. Here, the chairs the kept one on the other which shows that when you add a new chair (push) you should place it at the top of the stack and when you need to take, you should take (pop) it from the top. If you need to take the fifth chair from the top, you are supposed to take out the first four chairs and you get your fifth chair. This makes true for the statement, “You take the chair which you placed most recently” otherwise, * First In Last Out (FILO)* or

*. This concept is implemented in the data structure where the values can be placed in this order if we need.*

**Last In First Out (LIFO)****More on implementation on a Stack**

Stack has memory whenever implemented in a program. When the memory of the values pushes into a stack gets collectively more than the memory of the stack then the problem occurs as **Stack Overflow***. *If n values stored in the stack get popped from it and when popping the non-existing (n+1)th value then the problem occurs as **Stack Underflow***.*

**Implementation of Stack in Python**

class Stack:

def __init__(self):

self.items = []

def push(self, item):

self.items.append(item)

def pop(self):

return self.items.pop()

*: O(n) when accessing and searching values in the stack, O(1) when inserting or deleting a value in the stack.*

**Time Complexity***: O(n)*

**Space Complexity**Like and Follow this blog. Support our Facebook page. Feel free to contact us for your queries. Mail us your queries to **askregins@gmail.com**

From,

Maria Irudaya Regilan J