Explain Stack
It is a linear data structure or ordered collection of elements where the elements are processed in last in first out manner(LIFO).
In stack insertion and deletions are made at one end i.e top
Stack can be implemented by using array or linked list
Operations on stacks.
Special terminology is used for two basic operations associated with stacks are,
a. ”push” is the term used to insert an element into stack.
b. ”pop” is the term used to delete an element from stack.
PUSH operation.
- When top=-1 stack is empty i.e there are no elements in the stack
- After inserting first element top is incremented to 1st position(0th index)of stack
- Similarly top will get increment BY ONE after inserting every element in to stack
- When top value become MAXSTK-1(where MAXSTK is maximum number of elements that stack can have) stack is full that condition is called as stack overflow.
POP operation.
- If top ≠ -1 the top element of stack is deleted, otherwise stack is empty
- When an element of the stack is deleted top get decremented by one
- When top=-1 stack will become empty and that condition is called stack underflow
Array Representation of stacks:
- Stacks may be represented in the computer in various ways,normally by using linear array or
Linked list. - Unless we specified or stated ,each of our stacks will be maintained by a linear array STACK.
- A pointer variable TOP ,which contains the location of the top element in the stack
And a variable MAXSTK which gives the maximum number of elements that can STACK
have. - The condition TOP=-1 or TOP=NULL will indicate that the stack is empty
- The bellow array can be used to perform stack operations like push and pop