Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Stacks as ADTs Data – A collection of elements of the same type Operations – clear() – isEmpty() – push(el) – pop() – topEl()

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

4 Stacks as ADTs (cont’d)

5 Applications of Stacks parenthesis matching Performing “postfix” calculations Performing “infix” to “postfix” conversions Function calls (and specially recursion) rely upon stacks

6 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

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

8 When will an error reported for Symbol Matching

9 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

10 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 * +

11 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 * +

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

13 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 * +

14 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

15 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

16 Example


Download ppt "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."

Similar presentations


Ads by Google