Stacks is a kind of data-structure that hold elements in a linear fashion. The principle followed is LIFO which stands Last-In-First-Out. The latter means that , the element inserted at last in stack would be removed first from same. Further the famous pointer “top” is used to highlight the top most element of the stack or last inserted element in stack.
Linked list is a type of data-structure that holds elements through non contagious memory locations. The memory units are called as nodes that hold the data along with the address of the next unit. This way, we can save chunks of memory and increase the efficiency of the component. The address here acts as a link which helps user to traverse data , from one node to other.
[imagecredit]https://codeforwin.org/wp-content/uploads/2015/09/Linked-list-nodes.png
The biggest advantage of implementing stack using linked list is , that it allows one to change the size of stack in runtime. The implementation begins by considering a linked list attached within a stack. The top pointer will be the lead to create a stack as it will move along the new inserted nodes.
- PUSH: To insert element into stack using top pointer.
- POP: To remove element from stack using top pointer.
- PEEK: Returns the element pointed by top pointer.
- DISPLAY: To display the element pointed by stack.
- SPLIT: Split the stack of length n in two stacks, each of length p and q such that p+q=n.
- COMBINE: Combine((p(i1,i2,i3...ip),q(j1,j2,...jq)) into one stack of length p+q=n. The new stack should contain the elements of the stacks p and q in any combination.
Tested in online compiler: