stacks a stack is a linear data structure that can be accessed only at one of its ends for storing...

16
Stacks • A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Upload: megan-goodman

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

Stacks as ADT’s (cont’d) Implementation – With a doubly linked list, code in book Time Complexity of push and pop operations?

TRANSCRIPT

Page 1: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Stacks

• A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data

• LIFO (Last In First Out) structure

Page 2: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Stacks as ADTs• Data

– A collection of elements of the same type

• Operations

– clear()– isEmpty()– push(el)– pop()– topEl()

Page 3: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Stacks as ADT’s (cont’d)

• Implementation– With a doubly linked list, code in book

Time Complexity of push and pop operations?

Page 4: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Stacks as ADTs (cont’d)

Page 5: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Applications of Stacks

• parenthesis matching• Performing “postfix” calculations• Performing “infix” to “postfix” conversions• Function calls (and specially recursion) rely

upon stacks

Page 6: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Symbol Balancing

• A useful tool for checking your code is to see if the (), {}, and [] symbols balance properly

• For example, the sequence …[…(…)…]… is legal but the sequence …[…(…]…)… is not

• The presence of one misplaced symbol can result in hundreds of worthless compiler diagnostic errors

Page 7: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Symbol Balancing (cont’d)• The algorithm is simple and efficient:

– Create an empty stack– Read characters until end of file (EOF)– If the character is an opening symbol (, [, or {, push it onto the stack– If it is a closing symbol ), ], or }, then if the stack is empty report an error.

Otherwise pop the stack– If the symbol popped is not the corresponding opening symbol, report an

error– At EOF, if the stack is not empty report an error

• Example: Apply this algorithm to the statement:(([])), (([)])

s=t[5]+u/(v*(w+y));

Page 8: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

When will an error reportedfor Symbol Matching

Page 9: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Postfix Evaluation

• What is “postfix”?– It is the most efficient method for representing

arithmetic expressions– With “infix” (the method you are used to) a + b operators between operands:– With “postfix” : a b + operators after operands– There is never any need to use ()’s with postfix

notation. There is never any ambiguity

Page 10: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Postfix Example

• This example is in infix:a + b * c + (d * e + f) * g

• In postfix this is:a b c * + d e * f + g * +

Page 11: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Postfix Evaluation Algorithm

• The algorithm is simple and efficient O(n):– Read in input– If input is an operand, push on stack– If input is an operator, pop top two operands off stack,

perform operation, and place result on stack

• Example: 1+2*3 (infix)123*+ (postfix)a b c * +

Page 12: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Infix to Postfix Conversion

• Only need a stack to write an algorithm to convert an infix expression to a postfix expression– It can handle the presence of ()’s– It is efficient: O(n)

Page 13: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Infix -> Postfix Conversion Example

• This example is in infix:a + b * c + (d * e + f) * g

• In postfix this is:a b c * + d e * f + g * +

Page 14: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Conversion Algorithm• The algorithm is reasonably simple:

– Read infix expression as input– If input is operand, output the operand– If input is an operator +, -, *, /, then pop and output all operators of >=

precedence. Push operator– If input is (, then push– If input is ), then pop and output all operators until see a ( on the stack. Pop

the ( without output– If no more input then pop and output all operators on stack

Page 15: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Function Calls

• In almost all programming languages, calling a function involves the use of a stack– When there is a function call, all of the important

information (e.g., values of variables, etc…) is stored on a stack, and control is transferred to the new function

– This is especially important during recursion

Page 16: Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure

Example