Memory Representation of Stacks
In this article, we'll take a look at the various methods to represent stacks in memory.
There are two ways to represent stacks in memory:- Array representation (Static data structure)
- Linked list representation (Dynamic data structure)
Array representation of Stacks
Stacks may be represented in the computer in various ways, usually by means of a one-way list or a linear array. Unless otherwise stated or implied, each of our stacks will be maintained by a linear array STACK;a pointer variable TOP, which contains the location of the top element of the stack; and a variable MAXSTK which gives the maximum number of elements that can be held by the stack. The condition TOP = 0 or TOP = NULL will indicate that the stack is empty.
1. Insertion in Stack
Algorithm: (Insert an element into Stack.) PUSH(STACK, TOP, MAXSTK, ITEM).
This algorithm pushes an ITEM onto a stack.
Step I: [OVERFLOW?]
If TOP = MAXSTK, then: Print: OVERFLOW, and Exit.
Step II: Set TOP := TOP + 1. [Increases TOP by 1.]
Step III: Set STACK[TOP] := ITEM. [Inserts ITEM in new TOP position.]
Step IV: Exit.
You May Also Like: Anmol Vachan in Hindi
This algorithm deletes the top element of STACK and assigns it to the variable ITEM.
Step I: [UNDERFLOW?]
If TOP = NULL, then: Print: UNDERFLOW, and Exit.
Step II: Set ITEM := STACK[TOP]. [Assigns TOP element to ITEM.]
Step III: Set TOP := TOP – 1. [Decreases TOP by 1.]
Step IV: Exit.
If TOP = MAXSTK, then: Print: OVERFLOW, and Exit.
Step II: Set TOP := TOP + 1. [Increases TOP by 1.]
Step III: Set STACK[TOP] := ITEM. [Inserts ITEM in new TOP position.]
Step IV: Exit.
You May Also Like: Anmol Vachan in Hindi
2. Deletion
Algorithm: (Delete element from Stack.) POP(STACK, TOP ITEM).This algorithm deletes the top element of STACK and assigns it to the variable ITEM.
Step I: [UNDERFLOW?]
If TOP = NULL, then: Print: UNDERFLOW, and Exit.
Step II: Set ITEM := STACK[TOP]. [Assigns TOP element to ITEM.]
Step III: Set TOP := TOP – 1. [Decreases TOP by 1.]
Step IV: Exit.
You May Also Like: xvideosxvideostudio video editor pro apkeo
Post a Comment