0% found this document useful (0 votes)
373 views17 pages

Data Structures and Algorithm: Stacks

This document discusses stacks and their applications in data structures and algorithms. It defines a stack as a list that only allows insertions and deletions at one end, called the top. Common stack operations are push, pop, top, and isEmpty. Stacks can be implemented using linked lists or arrays. The document also discusses how stacks can be used to check if delimiters like parentheses are balanced in expressions, and how stacks are used to evaluate postfix expressions by applying operators to popped operands. It provides algorithms to convert infix expressions to postfix and to evaluate postfix expressions using a stack.

Uploaded by

Mesfin Mathewos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
373 views17 pages

Data Structures and Algorithm: Stacks

This document discusses stacks and their applications in data structures and algorithms. It defines a stack as a list that only allows insertions and deletions at one end, called the top. Common stack operations are push, pop, top, and isEmpty. Stacks can be implemented using linked lists or arrays. The document also discusses how stacks can be used to check if delimiters like parentheses are balanced in expressions, and how stacks are used to evaluate postfix expressions by applying operators to popped operands. It provides algorithms to convert infix expressions to postfix and to evaluate postfix expressions using a stack.

Uploaded by

Mesfin Mathewos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

DATA STRUCTURES AND

ALGORITHM
Stacks
STACK - DEFINITIO
 A stack is a list with the restriction that insertions and
deletions can be performed in only one position, namely,
the end of the list, called the top.
 Resembles a stack of trays in a cafeteria: New trays are
put on the top of the stack and taken off the top.
 The last tray put on the stack is the first tray removed
from the stack.
 A last in first out (LIFO) structure
STACK - OPERATIONS
 push(el): put an element on top of stack
 pop(): return the top most element from the stack and
remove it from the list.
 top(): return the topmost element in the stack without
removing it. (Some times called peek() )
 isEmpty(): checks if the stack contains any items.
IMPLEMENTATION OF STACKS
 Linked List implementation
 You simply insert and delete nodes from the head.
 Array Implementation
 You save the current position of the top item.
BALANCING DELIMITERS
 Most programming languages have delimiters such as “{
}”, “( )”, “[]”.
 For example, C-style languages like Java use “{ }” to
delimit methods, classes, and other units
 If these delimiters are mismatched, the program is
considered incorrect
 Example: the expression 1 + 2((3 + 2 ∗ 2 − 5) has a missing
“)”.
 Compilers have to check for such malformed
expressions.
 Stacks are perfect for doing this
ALGORITHM FOR CHECKING IF DELIMITERS ARE
BALANCED

1. Read characters until end of line.


2. If the character is an opening symbol, push it to the
stack.
3. If it is a closing symbol:
 Ifthe stack is empty report an error.
 Otherwise, pop from the stack. If the symbol popped is not
the corresponding opening symbol, then report an error.
4. At end of file, if the stack is not empty report an error.
EVALUATING EXPRESSIONS -
BACKGROUND
 We normally write arithmetic expressions as:
(3 + 5) ∗ 7/2 − 4
 This is called “infix” notation

 Unless we strictly follow the rules for the precedence of


operators, we will get wrong answers
 Infix notation is not the most convenient notation for
computers
 We have to program all the logic needed to consider
operator precedence and parenthesis rules
EVALUATING EXPRESSIONS -
BACKGROUND(2)
 The so called “postfix” notation is more convenient for
computers
 In
postfix notation, a + b is written as ab+
 More generally, operators follow their operands

 Example: a + b ∗ c becomes abc ∗ +


 The“∗” operator is first applied to b and c
 Then, the “+” operator is applied to result of (bc ∗) and a

 a + b ∗ c + (d ∗ e + f ) ∗ g can be converted to
abc ∗ +de ∗ f + g ∗ +
 A Postfix expression does not need any parenthesis. It
also internally represents precedence of operations
WHY POSTFIX?
 Postfix expressions are not ambiguous like their infix
counter parts
 This makes them more convenient for programming

 Compilers and other expression evaluators convert infix


expressions to postfix before evaluating them.
 Once we have the infix expression we can use the stack
ADT to efficiently evaluate them.
ALGORITHM FOR POSTFIX EXPRESSION
EVALUATION
1. Start scanning the expression from left to right
2. When a number is seen, it is pushed onto the stack.
3. When an operator is seen, the operator is applied to the
two numbers (symbols) that are popped from the stack,
and the result is pushed onto the stack.
4. When we reach the end of the expression, the final
result can be popped from the stack.
POSTFIX EXPRESSION EVALUATION
EXAMPLE
 Evaluating 8 + (1 + 2) ∗ 4
 In postfix: 8 1 2 + 4 ∗ +

 Users input is normally entered in infix notation


 Stacks can also be used to convert them to postfix
ALGORITHM FOR CONVERTING AN
INFIX TO POSTFIX
if operand is found then
append it to output
else
if operator is found then
if operators on top of stack have equal or higher precedence then
pop them out and append it to output
end if
place current operator on stack
end if
else
if “(” is found then
push it to stack
end if
else
if “)” is found then
pop all operators to output until “(” is found discard “(”
end if
end if
when finished scanning text pop remaining operators to output
INFIX TO POSTFIX EXAMPLE
 Converting a + b ∗ c + (d ∗ e + f ) ∗ g
INFIX TO POSTFIX EXAMPLE -
CONTINUED
 Converting a + b ∗ c + (d ∗ e + f ) ∗ g – Continued
FUN FACT
 Current scientific calculators use the previous algorithm
for converting an infix expression to postfix
 old scientific calculators (like the one shown on the
below)required users to enter expressions in postfix
form!
ADDITIONAL ASSIGNMENT FOR
WEDNESDAY
 Write a code that is used to reverses a word
 Example : if you give the code the word “GOOD” the
output should be ‘”DOOG”

You might also like