operator stack

advertisement
Comp 245
Data Structures
Stacks
What is a Stack?



A LIFO (last in, first out) structure
Access (storage or retrieval) may only
take place at the TOP
NO random access to other elements
within the stack
An Abstract view of a Stack
PUSH and POP

Push
Method which will store data sent from the
application onto the stack. Where this data is
placed now becomes the top of the stack.

Pop
Method which will remove the data found at the top
of the stack and will send it to the application.
The top of the stack will be changed to the next
element in the stack.
A RRRRobust Stack

Full
Before push can function, you must assure
there is room for the data to be pushed!! This
can be implemented as a separate method or
incorporated into the push function.

Empty
Before pop can function, you must assure there
is data to be popped!! This can be implemented
as a separate method or incorporated into the
pop function.
The Palindrome Problem
A palindrome is defined as text which if written backwards would
be the exact same text.
Examples:

1234321

BOB

ABLE WAS I ERE I SAW ELBA
Note – In this definition, spaces matter. Some definitions will strip
spaces and just take the raw text.
Problem – Write a function which will take a string on input and
will output if this string IS or IS NOT a palindrome. The
solution must utilize a stack ADT when executing the
solution.
The Palindrome Problem
Utilizing Stacks in the Solution
Step 1 – Instantiate Two Stacks, S1 and S2
Step 2 – Push string onto S1
Step 3 – Pop half of S1 onto S2
Step 4 – If length of string is odd, an extra character
will be in S1, pop this and trash
Step 5 – Pop S1 and S2 and compare popped values
Step 6 – If values are equal go back to Step 5
assuming S1 and S2 are not empty (if they are
empty go to step 7); however, if values are unequal,
string is not a palindrome, go to step 7
Step 7 – Output if the string IS or IS NOT a palindrome
Stack Implementation
Array Based
A Stack object will
contain:
An array of StackTypes: Data
An integer control field: Top

top should always
indicate the first
available slot
where data may be
placed

top will also
indicate whether or
not the stack is
empty or full
Stack Implementation
Linked List Based

Only data needed is a pointer to the top of the
linked list.

Very efficient, you are always pushing and
popping from the top. There is no list traversal!!

An empty condition is when top = NULL.

A full condition is when you cannot obtain dynamic
memory.
Stack Implementation
PUSHing a Linked List
Stack Implementation
POPping a Linked List
Defining Stack Operations
Functionality
//Constructor
Stack();
//Destructor
~Stack();
//Push Data – return if successful or not – full functionality
bool Push (StackType);
//Pop Data – return if successful or not – empty functionality
bool Pop (StackType&);
Defining Stack Data
Array Based

An array of some
set size – this is
where the stack
data is kept.

An integer field to
be used to mark
the top.
Linked List Based

A pointer which
contains the
address for the top
node in the stack.
Stack Application
Reverse Polish Notation

Arithmetic expressions are normally written in infix notation. It is called infix
because the arithmetic operator (i.e. +, -, *, /) is in-between the operands.

Example:
5+3*2–6

The answer above is 5. It is difficult to develop an algorithm to evaluate infix
expressions due to precedence problems. You cannot simply evaluate
an expression straight left to right!

Part of the task of a compiler is to generate machine language instructions to
carry out the evaluation of an arithmetic expression.
Ex. Z = a + b * c – d;

If we could evaluate an arithmetic expression by simply going straight left to
right, the task of the compiler would be much easier.
Stack Application
Reverse Polish Notation

A polish logician developed a way in which
an arithmetic expression could be written
that would allow it to be evaluated straight
left to right. He called it reverse polish
notation. Most people prefer to call this
notation postfix.

This notation will…
•
•
Eliminate precedence (thus allowing a left to
right evaluation)
Eliminate parenthesis
Stack Application
Reverse Polish Notation
Examples
Infix
a.
3+2
a.
Postfix
32+
b.
3+2*4
b.
324*+
c.
3 + 2 * 4 – (5 + 2)
c.
324*+52+-
Stack Application
Reverse Polish Notation
Evaluation

Requires the usage of an operand stack.

A postfix expression consists of two types of tokens;
operators and operands.

Steps:
•
•
•
•
Scan expression left to right
If token is an operand then push
If token is an operator then pop two, evaluate and push result
If the postfix expression was correctly formed then when all
tokens have been processed there should be one element
remaining on the stack; this should be the answer.
Stack Application
Reverse Polish Notation
Evaluation Practice
1)
345+Answer: -6
2) 6 1 - 3 * 5 7 8 / + +
Answer: 18
3) 2 6 + 5 * Answer: invalid expression
Stack Application
Reverse Polish Notation
Infix to Postfix Conversion


Requires usage of an operator stack and postfix
string.
Steps:
•
•
If token is an operand, push onto postfix string.
If token is an operator, compare this with the top of the
operator stack
o
o
o
o
If token is lesser (precedence), pop the operator stack and
push onto postfix string then revaluate
If token is greater(precedence), push onto the operator
stack
If token is equal (precedence) use the lesser rule
Comparing against an empty operator stack will always
result in a push onto the operator stack
Stack Application
Reverse Polish Notation
Infix to Postfix Practice
1)
2)
2+3*4
Answer: 2 3 4 * +
A + (B – D) * E – F
Answer: A B D – E * + F -
C++ STL Stack Container
Class

The C++ STL stack container is a LIFO structure
where elements are inserted and removed only
from the end (top) of the container.
http://www.cplusplus.com/reference/stl/stack/

Here is an example of the palindrome problem
using the STL stack.
PaliSTL.cpp
Download