Today we will be discussing about What are Stacks ? - data structure tutorial.
Stack is a linear data structure in which elements are inserted in LIFO(last in first out) manner. A real life examples of stacks is pile of plates. The plate which was inserted first will be removed last and the plate which was placed at the top will be removed first.
Basic operations performed on a stack
1. Push() : This function is used to push/insert elements into the stack.
2. Pop() : This function is used to pop/delete an element from stack.
3. Top() : This function returns the top-most element of the stack.
- When a stack is filled completely then it is known as stack overflow. When a stack is completely empty then it is known as stack underflow (What are Stacks in Data Structure?).
- Stacks allow its operations to be performed only at one end. For example, you can only insert a plate at the top of pile of plates or you can only remove a plate from top of pile of plates.
Implementation of Stack operations
- Insertion is performed using push() function. Initially, the stack is empty and top is set '-1'.
- When an element is inserted into the stack then top is incremented and then the element is inserted at the location top is pointing to.
- This process continues until value of top exceeds the size of array.
- Deletion is performed using pop() function.
- When an element is to be deleted then we first print the element which is to be deleted if we want to print the element which is deleted. Otherwise, just decrement value of top.
- This process continues until top becomes less than 0 which denotes that the stack is now empty.
- Note : When top is decremented then the value is not actually removed from the array but we just consider that the value lies outside the range of top and thus it is removed(What are Stacks in Data Structure?) .
Code for implementing Stack
If you have understood the concept of stacks then try out these problems:
For an array which is sorted in decreasing order, all elements have next element as -1.
For the input array[4,5,2,25], the next greater elements for each element are as follows:
If you have understood the concept of stacks then try out these problems:
- WAP to convert infix expression to postfix.
- WAP to convert postfix expression to infix.
- WAP to solve The Stock Span Problem.
- Given an array, print the next Greater Element for every element. The Next greater element for an element x is the first greater element on the right side of x in array. Element for which no greater element exist ,consider next greater element an -1.
For an array which is sorted in decreasing order, all elements have next element as -1.
For the input array[4,5,2,25], the next greater elements for each element are as follows:
Element NGE
4
--> 5
5
--> 25
2
--> 25
25
--> -1
2.
So this was all about What are Stacks ? - data structure tutorial.
great content.i reallyy enjoyed goining through it.so nice of you that you invested your time and energy in writing this beautiful article.
ReplyDeleteThanks Shubham. Please share it with your friends and colleagues as well.
Delete