The Stack Data Structure
Introduction
A stack is an ordered collection of items into which insertion and deletion operations occurs at only one end, called top of the stack (TOS). Unlike that of the array, the definition of stack provides for the insertion and deletion of items so that stack is dynamic and constantly changing object.
Stacks are the subclass of lists that permits the insertion and deletion
operation to be performed at only one end.
So, how does stack change? The definition of stack specifies that a single end which is designated as the stack top. New items may be put on top of the stack where TOS moves upward and items which are at top of the stack may be removed in which TOS moves downward.
Here insertion and deletion is done from the same end, last element inserted will be the first element to be removed out of the stacks resulting FILO (First In Last Out) or LIFO (Last In First Out). It can be implemented into two ways:
1. Static implementation
- Using array
2. Dynamic implementation
- Using linked list
Operations on stacks
1. isFull()
- if TOS == max-1, return true
- else return false
2. isEmpty()
- if TOS == -1, return true
- else return false
3. push()
- if isFull(), then display "stack is full" and stop
- read data
- TOS = TOS + 1
- stack [TOS] = data
- stop
4. pop()
- if isEmpty(), then display "stack is empty" and stop
- item = stack [TOS]
- TOS = TOS - 1
- return item
Application of Stack
- Used to process function calls
- Implementing recursive functions in high level languages
- To check the validity of an arithmetic expression
- Evaluating and converting the mathematical expression
- Counting parenthesis and matching nested parenthesis
- Maintaining browsing history
- For undo operations
- String reversing
- Interop handling in microprocessor etc.

Comments
Post a Comment