What is Stack in Data Structure ?
- A stack is a container of objects that are performed based on last-in first-out (LIFO) principle.
- A stack is a limited access data structure-elements can be added and removed from the stack only at the top. A stack is a recursive data structure.
- The structural definition of a Stack is either empty or it consists of a top and the rest.
- There are two basic operations performed in a Stack they are:
-
- Push()
- Pop()
-
- Push()-push function is used to add or insert new item into the stack.
-
- Pop()-pop function is used to delete or remove an item from the stack.
- When a stack is completely full, it is said to be Overflow state and if stack is completely empty, it is said to be Underflow state.
- Stack allows operations at one end only.
Other operations used in Stack
- Peek()-The peek() function gets the top element of the stack, without deleting it.
- isEmpty() -The isEmpty() function checks whether the stack is empty or not.
- isFull()-The isFull() function is used to check whether the stack is full or not.
Features of stacks
- Dynamic data structures
- Do not have a fixed size
- Do not consume a fixed amount of memory
- Size of stack changes with each push() and pop() operation. Each push() and pop() operation increases and decreases the size of the stack by 1, respectively.