Unit Two
Unit Two
The Stack
What is Stack??
• A stack is a linear data structure in which the insertion of a new element and
removal of an existing element takes place at the same end represented as the top
of the stack.
• To implement the stack, it is required to maintain the pointer to the top of the
stack, which is the last element to be inserted because we can access the elements
only on the top of the stack.
• Stack has one end, whereas the Queue has two ends (front and rear). It contains
only one pointer top pointer pointing to the topmost element of the stack.
Whenever an element is added in the stack, it is added on the top of the stack, and
the element can be deleted only from the stack. In other words, a stack can be
defined as a container in which insertion and deletion can be done from the one
end known as the top of the stack.
Some key points related to stack
• It is called as stack because it behaves like a real-world stack, piles of books, etc.
• A Stack is an abstract data type with a pre-defined capacity, which means that it
can store the elements of a limited size.
• It is a data structure that follows some order to insert and delete the elements, and
that order can be LIFO or FILO.
Basic Operations on Stack
• In order to make manipulations in a stack, there are certain operations provided to
us.
• push() to insert an element into the stack
• pop() to remove an element from the stack
• top() Returns the top element of the stack.
• isEmpty() returns true if stack is empty else false.
• size() returns the size of stack.
Stack as ADT
• In Stack ADT Implementation instead of data being stored in each node, the pointer to data is
stored.
• The program allocates memory for the data and address is passed to the stack ADT.
• The head node and the data nodes are encapsulated in the ADT. The calling function can only
see the pointer to the stack.
• The stack head structure also contains a pointer to top and count of number of entries
currently in stack.
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is not empty.
• peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
Implementation of Stack
Steps:
1. Initially, we get empty stack. The top of an empty stack is set to -1.
2. Next, we push an element into the stack. The top of the stack will now point to the element
just pushed.
3. Whenever, we push element into the stack, the top is incremented so as to point to the
recent element.
4. Whenever a pop operation is performed, the top element is removed from the stack and top
is decrement to point to the next element.
5. If the stack becomes empty, the top will be set to -1.
• A stack data structure can be implemented using a one-dimensional array. But stack
implemented using array stores only a fixed number of data values.
Algorithm InitializeEmptyStack(S)
1. Start
2. Assign the value to top to -1. ie S->top = -1
3. Stop
Algorithm isFull(S)
4. Start
5. Check if the top of S is equal to MAX-1, return true
ie
if(s->top == MAX-1)
return true;
6. Else return false
7. Stop
Algorithm isEmpty(S)
1. Start
2. Check if the top of S is equal to -1, return true
ie
if(S->top == -1)
return true;
3. Else return false
4. Stop
Applications
Stack is used directly and indirectly in the following fields:
● To evaluate the expressions (postfix, prefix)
● To keep the page-visited history in a Web browser
● To perform the undo sequence in a text editor
● Used in recursion
● To pass the parameters between the functions in a C program
● Can be used as an auxiliary data structure for implementing algorithms
● Can be used as a component of other data structures
Stack Implementation
Steps:
1. Initially, we get empty stack. The top of an empty stack is set to -1.
2. Next, we push an element into the stack. The top of the stack will
now point to the element just pushed.
3. Whenever, we push element into the stack, the top is incremented so
as to point to the recent element.
4. Whenever a pop operation is performed, the top element is removed
from the stack and top is decrement to point to the next element.
5. If the stack becomes empty, the top will be set to -1.
Continued….
Infix, Prefix and Postfix Notation
First convert the sub-expression to postfix that is to be evaluated first and repeat
this process.
Substitute intermediate postfix sub-expression by any variable whenever
necessary that makes it easy to convert.
To convert an infix expression to its postfix equivalent:
We first convert the innermost parentheses to postfix, resulting as a new
operand
In this fashion parenthesis can be successively eliminated until the entire
expression is converted
The last pair of parenthesis to be opened within a group of parenthesis
encloses the first expression within the group to be transformed
This last in, first-out behavior suggests the use of a stack
Precedence rule:
While converting infix to postfix you have to consider the precedence rule, and
the precedence rules are as follows:
Exponentiation ( the expression AB is A raised to the B power, so that 32
=9)
Multiplication/Division
Addition/Subtraction
When un-parenthesized operators of the same precedence are scanned, the order
is assumed to be left to right except in the case of exponentiation, where the
order is assumed to be from right to left.
A+B+C means (A+B)+C
A^B^C means A^(B^C)
By using parenthesis we can override the default precedence. Ex: A + (B* C).
Rules for the conversion from infix to postfix expression
1. Print the operand as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming
operator on to the stack.
3. If the incoming symbol is '(', push it on to the stack.
4. If the incoming symbol is ')', pop the stack and print the operators until the left
parenthesis is found.
5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming symbol has lower precedence than the top of the stack, pop and print
the top of the stack. Then test the incoming operator against the new top of the stack.
7. If the incoming operator has the same precedence with the top of the stack then use
the associativity rules. If the associativity is from left to right then pop and print the
top of the stack then push the incoming operator. If the associativity is from right to
left then push the incoming operator.
8. At the end of the expression, pop and print all the operators of the stack.
Use the following rule to convert it in postfix:
1. Parenthesis for emphasis
2. Convert the multiplication
3. Convert the addition
4. Post-fix form
Algorithm
1. Add the unique symbol # at the end of array pstack.
2. Scan the symbol of postfix expression one by one from the left to right.
3. Do the following with each symbol until # is encountered:
a. If the symbol is operand, push into stack.
b. If the symbol is operator, the pop last 2 elements of the stack and evaluate it
as :
Pstack[top-1] operator pstack[top]
Push the result into the stack
4. Pop the element of the stack which will be the value of evaluation of postfix
expression.
Ex: Evaluation of
ABCD$+*EF$GH/*-
DO THIS:
123+*321-+* => ??
• Let us now consider another example. Suppose that we are asked to evaluate the
following postfix expression:
623 + — 382 -f-* 2$ 3 +
• We show the contents of the stack opndstk and the variables .cvnib, opndl , opnd2,
and value after each successive iteration of the loop. The top of opndstk is to the
right.
Evaluation of prefix expression
Algorithm:
1. Begin with initializing opndstack as an empty operand stack.
2. Scan infix expression one char at a time from right to left.
3. Do the following until no more characters in infix expressions.
a. If the scan char is operand, push it to opndstack
b. If the scan char is operator, pop opndstack[top] and opndstack[top-1] and
perform the specified operation as:
Opndstack[top] operator opndstack[top-1]
Push the result into stack
4. Pop opndstack and display the required value.
• Ex: -*+4325 PERFORM THIS: