Stack Data Structure
Stack Data Structure
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure
there are five memory blocks in the stack; therefore, the size of the stack
is 5.
Suppose we want to store the elements in a stack and let's assume that
stack is empty. We have taken the stack of size 5 as shown below in
which we are pushing the elements one by one until the stack becomes
full.
Top =0 Top=1 Top=2
Top=33 Top
Top=4
Since our stack is full as the size of the stack is 5. In the above cases, we
can observe that it goes from the top to the bottom when we were
entering the new element in the stack. The stack gets filled up from the
bottom to the top.
When we perform the delete operation on the stack, there is only one
way for entry and exit as the other end is closed. It follows the LIFO
pattern, which means that the value entered first will be removed last. In
the above case,
se, the value 10 is entered first, so it will be removed only
after the deletion of all the other elements.
Applications of Stack
1. Expression Evaluation
2. Expression Conversion
i. Infix to Postfix
ii. Infix to Prefix
iii. Postfix to Infix
3.Backtracking
4.Delimiter Checking
5.Reverse a Data
6.Processing Function Calls
Example: A + (B - C)
o Infix Notation
o Prefix Notation
o Postfix Notation
Infix Notation
Example: A + B, (C - D) etc.
All these expressions are in infix notation because the operator comes
between the operands.
Prefix Notation
The prefix notation places the operator before the operands. This notation was
introduced by the Polish mathematician and hence often referred to as polish
notation.
All these expressions are in prefix notation because the operator comes before
the operands.
Postfix Notation
The postfix notation places the operator after the operands. This notation is
just the reverse of Polish notation and also known as Reverse Polish notation.
All these expressions are in postfix notation because the operator comes after
the operands.
Conversion of Arithmetic Expression into various Notations:
Stack is the ideal data structure to evaluate the postfix expression because the
top element is always the most recent operand. The next element on the Stack
is the second most recent operand to be operated on.
Example:
3. Delimiter Checking
The common application of Stack is delimiter checking, i.e., parsing that
involves analyzing a source program syntactically. It is also called parenthesis
checking. When the compiler translates a source program written in some
programming language such as C, C++ to a machine language, it parses the
program into multiple individual parts such as variable names, keywords, etc.
By scanning from left to right. The main problem encountered while
translating is the unmatched delimiters. We make use of different types of
delimiters include the parenthesis checking (,), curly braces {,} and square
brackets [,], and common delimiters /* and */. Every opening delimiter must
match a closing delimiter, i.e., every opening parenthesis should be followed
by a matching closing parenthesis. Also, the delimiter can be nested. The
opening delimiter that occurs later in the source program should be closed
before those occurring earlier.
Valid Delimiter Invalid Delimiter
{ ( a + b) - c } { ( a + b) - c
o If the delimiters are of the same type, then the match is considered
successful, and the process continues.
o If the delimiters are not of the same type, then the syntax error is
reported.
When the end of the program is reached, and the Stack is empty, then the
processing of the source program stops.
o Reversing a string
o Converting Decimal to Binary
Reverse a String
A Stack can be used to reverse the characters of a string. This can be achieved
by simply pushing one by one each character onto the Stack, which later can
be popped from the Stack one by one. Because of the last in first
out propertyty of the Stack, the first character of the Stack is on the bottom of
the Stack and the last character of the String is on the Top of the Stack and
after performing the pop operation in the Stack, the Stack returns the String in
Reverse order.
5. Processing Fun
Function Calls:
Stack plays an important role in programs that call several functions in
succession. Suppose we have a program containing three functions: A, B, and
C. function A invokes function B, which invokes the function C.
When we invoke function A, which contains a call to function B, then its
processing will not be completed until function B has completed its execution
and returned. Similarly for function B and C. So we observe that function A will
only be completed after function B is completed and function B will only be
completed after function C is completed. Therefore, function A is first to be
started and last to be completed. To conclude, the above function activity
matches the last in first out behavior and can easily be handled using Stack.
Expression Representation
There are three popular methods used for representation of an
expression:
3 Empty 3
* * 3
3 * 33
/ / 33*
( /( 33*
4 /( 33*4
- /(- 33*4
1 /(- 33*41
) - 33*41-
+ + 33*41-/
6 + 33*41-/6
* +* 33*41-/62
2 +* 33*41-/62
Empty 33*41-/62*+
Implementation:
There are two ways to implement a stack:
Using array
Using linked list
Push Operation
The process of putting a new data element onto stack is known as a Push
Operation. Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, then Display message “stack is
OVERFULL” and exit.
Step 3 − If the stack is not full, increments top to point next empty
space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
End If
Step 2: Top=Top+1
Step 3: Stack [ TOP ]=Element
Step 4: End
End if
Step 3: Top=Top-1
Step 4: Del_Element
Step 5: End