Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access...

21
Comp 245 Data Structures Stacks

Transcript of Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access...

Comp 245Data 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

PushMethod which will store data sent from the

application onto the stack. Where this data is placed now becomes the top of the stack.

PopMethod 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

FullBefore 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.

EmptyBefore 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 ProblemUtilizing Stacks in the Solution

Step 1 – Instantiate Two Stacks, S1 and S2Step 2 – Push string onto S1Step 3 – Pop half of S1 onto S2Step 4 – If length of string is odd, an extra character

will be in S1, pop this and trashStep 5 – Pop S1 and S2 and compare popped valuesStep 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 ImplementationArray Based

A Stack object will contain:

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

An array of StackTypes: Data

An integer control field: Top

Stack ImplementationLinked 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 ImplementationPUSHing a Linked List

Stack ImplementationPOPping a Linked List

Defining Stack OperationsFunctionality

//ConstructorStack();

//Destructor~Stack();

//Push Data – return if successful or not – full functionalitybool Push (StackType);

//Pop Data – return if successful or not – empty functionalitybool 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 ApplicationReverse 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 ApplicationReverse 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 ApplicationReverse Polish NotationExamples

Infix

a. 3 + 2

b. 3 + 2 * 4

c. 3 + 2 * 4 – (5 + 2)

Postfix

a. 3 2 +

b. 3 2 4 * +

c. 3 2 4 * + 5 2 + -

Stack ApplicationReverse Polish NotationEvaluation

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 ApplicationReverse Polish NotationEvaluation Practice

1) 3 4 5 + -Answer: -6

2) 6 1 - 3 * 5 7 8 / + +Answer: 18

3) 2 6 + 5 * -Answer: invalid expression

Stack ApplicationReverse Polish NotationInfix 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 stacko If token is lesser (precedence), pop the operator stack and

push onto postfix string then revaluateo If token is greater(precedence), push onto the operator

stacko If token is equal (precedence) use the lesser ruleo Comparing against an empty operator stack will always

result in a push onto the operator stack

Stack ApplicationReverse Polish NotationInfix to Postfix Practice

1) 2 + 3 * 4

Answer: 2 3 4 * +

2) 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