Q. What is the difference between an array and a stack housed in an array? Why stack is called a LIFO data structure? Explain how push and pop operations are implemented on a stack.
Answer =
An array is a group of homogeneous elements stored in contiguous memory locations. The elements in an array can be processed from anywhere in the array.
A stack implementation as an array also has elements stored in contiguous memory locations. But a stack is always processed in a LIFO manner, i.e., Last In First Out manner wherein the elements can be added or removed from a top end of the stack. That is why a stack is also called a LIFO data structure.
An addition to the stack is known as PUSH. The new element is added at the top and the top (variable of pointer) is made to refer to the new element.
The removal of an element from a stack is known as POP. The element (being pointed to by top) is removed and the top is made to point to the next element in the row.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )