Stack is a LIFO (Last in first out) data structure.
- pop() – adds an element in the stack.
- push() – removes an element from the stack.
- peek() – get the top element without removing it.
- isEmpty() – checks whether stack is empty or not.
- isFull() – checks whether stack is full or not.
There are 2 ways to implement a stack.
- Array-based Stack
- Linked List based Stack
- Easier to use.
- Less memory allocation because no need to track the next node.
- Fixed size – can not increase or decrease the array.
Link List based Stack
- Easily increase or decrease the Stack size.
- Requires extra memory to keep details about next node.
Original post : - http://mydevgeek.com/stack-data-strurcture/
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.