Like an Array, Stack is an ordered collection of elements here the insertion and deletion of element take place at the same point. A stack is one of the most common Abstract Data Structures we use in programming.
In the real world we often use Stack Data Structure for example when we use a browser to surf the internet, on browser we get two buttons forward and backward icons, which help us to navigate to the previous URLs, this structure is made up of using Stack concept.
In our browser when we use the back icon to visit the previous page, the backward stack pops out the previous page and push the current page to the forward stack. When we want to go back to the forward page the browser pops the page from the forward stack and push the current page to the backward stack.
Stack Works on the LIFO principle which stands for Last In First Out, here the element pushed first in the stack would be accessed last and the element pushed last would be accessed first. For example, in the real world if there is a pile of plates, we will choose that plate which would be on the top of the pile. In other words, we will choose that plate which was put last on that pile.
- A Stack is an Ordered Collection of elements
- We insert and delete elements from the top of a stack.
- The first element of Stack lay on the base of that stack.
- The first element pushed in a Stack is known as Base element.
- The last element lay on the top of that stack.
- The recent added element would be the first element on which the removal operation works first.
- When we pop out elements from a stack it would be popped out in reverse order.
- In Stack, the order of Insertion is the reverse of the order of Removal.
Basic Operation on Stack:
Here are some basic Operations we apply on a Stack:
The Push operation pushes or inserts elements in the stack.
The pop operation removes the last added element of the stack.
The peek operation used to display the topmost data of the stack.
The Is_Empty function tells whether the stack is empty or not. If the stack consists even a single data element it returns True else it returns false.
Is_ Full operation test whether the stack is full or not, if the stack is full, we would not able to push more elements in the stack.
Advantages and Disadvantages of Stack
- In Array, we can not allocate memory dynamically but in Stack, we can do this.
- In stack, we can easily remove and insert elements.
- Stack is not that much flexible.
Implementation of Stack Using Python:
class Stack(object): def __init__(self,size): self.items= self.size =size def push(self,data): # Push operation inset element in the stack if len(self.items) < self.size: self.items.append(data) print(data,"has pushed") else: print("Stack is full") def pop(self): # pop function will remove the last added element self.items.pop() print("Element has been poped") def isempty(self): # isempty function will check whether the stack is empty or not print(len(self.items)==0) def isfull(self): # isfull function tell whether the stack is full or not print(len(self.items) ==self.size) def peek(self): # peek fucntion will return the top most element of the stack print(self.items[len(self.items)-1]) def display(self): # display will show all the elements present in stack for i in self.items[::-1]: print("|****|") print("|",i,"|") size = int(input("How many elements you want to enter in your stack: ")) stack = Stack(size) stack.push(10) stack.push(30) stack.push(22) stack.pop() stack.display() stack.isfull()
How many elements you want to enter in your stack: 3 10 has pushed 30 has pushed 22 has pushed Element has been poped |****| | 30 | |****| | 10 | False
People are also reading: