Stack is a linear data structure where items are kept according to the LIFO (Last In First Out) principle, meaning that the first element to be eliminated is the one that was added last.
Stack can be implemented using arrays as well as linked lists.
Array based stack : The components of the stack are stored in an array in this implementation. It is easy to use and provides quick access to items, but unless you use dynamic arrays, its size is fixed.
Linked list based stack : In this implementation, the elements are stored in a linked list. Its size is adjustable, but because of the pointers stored, there is an increased memory overhead.This will increase the flexibility of organizing and storing of the data in the linked list.
UML class diagram of a Stack-node
Structure of a Stack
UML classs diagram of a stack – array based
UML classs diagram of a stack – linked list based
1.2 Operations on stack
Push
Add an element to the top of the stack. Array based Linked list based
Pop
Remove the top element of the stack. Array based Linked list based
Peek
Get the top element without deleting.
IsEmpty
Check if the stack is empty
Printing the stack
print every element after iterating through the stack. Array based Linked list based