CACS201 Unit 2 - The Stack
CACS201 Unit 2 - The Stack
The Stack
Contents
• Introduction
• Stack as an ADT
• POP and PUSH Operation
• Stack Application
• Evaluation of Infix, Postfix and Prefix Expressions
• Conversion of Expression
Introduction
• A Stack is a linear collection of data elements
in which the element inserted last will be the
element taken out first (i.e., a stack is a LIFO
data structure).
• A stack is open only from one end.
• The stack is a linear data structure in which
the insertion, as well as deletion of an
element, is done only from the end called
TOP.
• One end is always closed, and the other end
is used to insert and remove data.
• Stacks can be implemented by using arrays or
linked lists.
Stack as an ADT
• A stack contains elements of the same type arranged in sequential
order and push and pop operations occur at one end at the top of the
stack. The following operations may be carried out on the stack:
• Push () – Insert an item on one end of the stack called the top.
• Pop () – Removes and returns the item to the top of the stack, if it is not
empty.
• Top () – Returns the element to the top of the stack without deleting it, if the
stack is not empty.
• Size () – Returns the number of items within the stack.
• IsEmpty () – Returns true if it is empty, otherwise returns false.
• IsFull () – Returns true if the stack is full or returns false.
Primitive Operations on Stack
1. To create a stack: Create a stack in memory. Creation of stack can be
done either using arrays or using linked lists.
2. To insert an element onto the stack that is push operation
3. To delete an element from stack that is the pop operation
4. To verify which element is at the top of the stack
5. To verify whether a stack is empty or full.
PUSH Operation
• Push operation is the process of adding new elements in the stack.
• However, before inserting any new element in the stack, we must
always check for the overflow condition, which occurs when we try to
insert an element in the stack which is already full.
• An overflow condition can be checked as follows, If TOP = MAX – 1,
where MAX is the size of the stack.
• Hence, if the overflow condition is true, then an overflow message is
displayed on the screen; otherwise, the element is inserted into the
stack.
PUSH Operation Example
Step 1: START
Step 2: Add “)” parenthesis to the end of infix expression.
Step 3: Push ’(’ parenthesis on the stack.
Step 4: Repeat the steps until each character in the infix expression is scanned.
a) IF “(” parenthesis is found, push it onto the stack.
b) If an operand is encountered, add it to the postfix expression.
c) IF “)” parenthesis is found, then follow these steps –
- Continually pop from the stack and add it to the postfix expression until a “(” parenthesis is encountered.
- Eliminate the “(” parenthesis.
d) If an operator is found, then follow these steps –
- Continually pop from the stack and add it to the postfix expression which has same or high precedence than
the current operator.
- Push the current operator to the stack.
Step 5: Continually pop from the stack to the postfix expression until the stack becomes empty.
Step 6: EXIT
Conversion of Infix Expression to Postfix Expression Example
• (A + B) *C / D
Convert
[((A +B) * (C – D)) + (F – G)]
to postfix expression.
Convert the following infix expression into a postfix
expression.
(A + B) ^ C – (D * E) / F
Write a program to convert an infix expression
to a postfix expression.
Algorithm to Convert Infix Expression to Prefix Expression
Step 1: START
Step 2: Reverse the infix expression. Also, interchange left and right
parenthesis on reversing the infix expression.
Step 3: Obtain the postfix expression of the reversed infix expression.
Step 4: Reverse the postfix expression so obtained in Step 3. Finally, the
expression is converted into prefix expression.
Step 5: EXIT
Conversion of Infix Expression to Prefix Expression Example
• (X - Y) / (A + B)
• After reversing the given infix
expression (B + A) / (Y – X)
• Find the postfix expression of
(B + A) / (Y – X)
• Conversion as shown here ->
• Now, reverse the postfix
expression so obtained, that
is, –X/Y+AB
• Hence, the prefix expression
is –X/Y+AB
Convert (X – Y / Z) * (A / B – C) to prefix
• After reversing the given infix
expression (C – B / A) * (Z / Y – X)
• Find the postfix expression of (C – B /
A) (Z / Y – X)
• ->
• Now, reverse the postfix expression so
obtained, that is, *–X/ZY-/ABC
• Hence, the prefix expression is *–
X/ZY-/ABC
Write a program to convert an infix expression
to a prefix expression.
Evaluation of a Postfix Expression
• With the help of stacks, any postfix expression can easily be
evaluated.
Evaluate the following postfix expressions
234+*5678+*++
Write a program for evaluation of a postfix
expression.
Evaluation of a Prefix Expression
Evaluate the given prefix expressions + - 4 6 * 9 /10 50
Write a program for evaluation of a prefix
expression.