What is a Stack?
Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. In a stack, adding and removing of elements are performed at a single position which is known as "top". That means, a new element is added at top of the stack and an element is removed from the top of the stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) principle.
In a stack, the insertion operation is performed using a function called "push" and deletion operation is performed using a function called "pop".
In the figure, PUSH and POP operations are performed at a top position in the stack. That means, both the insertion and deletion operations are performed at one end (i.e., at Top)
A stack data structure can be defined as follows...
Stack is a linear data structure in which the operations are performed based on LIFO principle.
Stack can also be defined as
"A Collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle".
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom-most element and 50 is the topmost element. The last inserted element 50 is at Top of the stack as shown in the image below...
Operations on a Stack
The following operations are performed on the stack...
- Push (To insert an element on to the stack)
- Pop (To delete an element from the stack)
- Display (To display elements of the stack)
Stack data structure can be implemented in two ways. They are as follows...
- Using Array
- Using Linked List
When a stack is implemented using an array, that stack can organize an only limited number of elements. When a stack is implemented using a linked list, that stack can organize an unlimited number of elements.