Welcome to CIS 068 Lesson 11 Data Structures

  • Slides: 25
Download presentation
Welcome to CIS 068 ! Lesson 11: Data Structures 2 CIS 068

Welcome to CIS 068 ! Lesson 11: Data Structures 2 CIS 068

Overview Description, Usage and Application of Queues and Stacks CIS 068

Overview Description, Usage and Application of Queues and Stacks CIS 068

Queues • FIFO (first in first out) Structure in 3 2 1 out CIS

Queues • FIFO (first in first out) Structure in 3 2 1 out CIS 068

Queues • Java Interface CIS 068

Queues • Java Interface CIS 068

Queues: Implementation 1 1. Implementation as Linked List in (null) 3 out 2 1

Queues: Implementation 1 1. Implementation as Linked List in (null) 3 out 2 1 CIS 068

Adding an Element in out (null) 1 (null) in out 2 1 Adding an

Adding an Element in out (null) 1 (null) in out 2 1 Adding an element E: • If in == null: in = E, out = E, E. link = null • else in. link = E, in = E, E. link = null CIS 068

Removing an Element in out 2 1 (null) in (null) 2 out in out

Removing an Element in out 2 1 (null) in (null) 2 out in out (null) Removing an element: • If out == null throw Empty. Queue. Exception • else • E=out, out=out. link • If out = null: in = null • return E CIS 068

Queues: Implementation 2 2. Implementation as Circular Array Capacity = 6 Index ! in

Queues: Implementation 2 2. Implementation as Circular Array Capacity = 6 Index ! in out 3 4 5 --1 2 size = 5 List-Size = 5 CIS 068

Circular Array: Adding an Element E out in 9 10 11 12 13 ---

Circular Array: Adding an Element E out in 9 10 11 12 13 --- E=14 in out size = 5 9 10 11 12 13 14 size = 6 If size == capacity return error (or increase capacity automatically) else array[in]=E size++ in = (in+1)%capacity CIS 068

Circular Array: Removing an Element out 9 10 11 12 13 --- in size

Circular Array: Removing an Element out 9 10 11 12 13 --- in size = 5 out in --10 11 12 13 --size = 4 If size == 0 throw Empty. Queue. Exception else E = array[out] size -out = (out+1)%capacity CIS 068

Stacks • LIFO (first in first out) Structure top 3 2 1 CIS 068

Stacks • LIFO (first in first out) Structure top 3 2 1 CIS 068

Stacks Java Interface CIS 068

Stacks Java Interface CIS 068

Stack: Implementation 1 1. Implementation as Linked List top (null) 1 2 3 CIS

Stack: Implementation 1 1. Implementation as Linked List top (null) 1 2 3 CIS 068

Adding an Element E (null) 1 2 top (null) 1 2 E=3 top Adding

Adding an Element E (null) 1 2 top (null) 1 2 E=3 top Adding an Element E: E. link = top = E CIS 068

Removing an Element (null) 1 2 3 (null) 1 2 top return 3 Removing

Removing an Element (null) 1 2 3 (null) 1 2 top return 3 Removing an Element E: if top==null throw Empty. Queue. Exception else E=top = top. link return E CIS 068

Stack: Implementation 2 2. Implementation as Array Capacity = 6 Index ! top ----4

Stack: Implementation 2 2. Implementation as Array Capacity = 6 Index ! top ----4 3 2 1 CIS 068

Adding / Removing top ----4 3 2 1 Adding an Element E: If top

Adding / Removing top ----4 3 2 1 Adding an Element E: If top == capacity return error (or increase capacity automatically) else array[top] = E top ++ Removing an Element: If top == 0 throw Empty. Queue. Exception else top -return array[top] CIS 068

Application 1 Checking for Balanced Parantheses Task: • Determine whether expression is balanced with

Application 1 Checking for Balanced Parantheses Task: • Determine whether expression is balanced with respect to parantheses • Allow for different symbols of parentheses: • () • [] • {} CIS 068

Algorithm CIS 068

Algorithm CIS 068

Algorithm Expression: (a+{b-c}*[d+(a+b)) Stack: • ({ • ( Error ! • ([( • ([

Algorithm Expression: (a+{b-c}*[d+(a+b)) Stack: • ({ • ( Error ! • ([( • ([ CIS 068

Application 2 Evaluating a Postfix Expression Postfix expression: Why ? No brackets necessary !

Application 2 Evaluating a Postfix Expression Postfix expression: Why ? No brackets necessary ! CIS 068

PN / RPN Prefix / Postfix Expression History • PREfix Notation (also called Polish

PN / RPN Prefix / Postfix Expression History • PREfix Notation (also called Polish Notation) invented in the 1920's by Polish mathematician Jan Lukasiewicz, • writing operators in front of their operands, instead of between them, makes brackets unnecessary • Postfix Expression = Reverse Polish Notation (RPN): operators appear behind their operands • Proposed in the late 1950's by the Australian philosopher and early computer scientist Charles L. Hamblin • Advantage: the operators appear in the order required for computation. CIS 068

HP 9100 A Engineers at the Hewlett-Packard company realised that RPN could be used

HP 9100 A Engineers at the Hewlett-Packard company realised that RPN could be used to simplify the electronics of their calculators at the expense of a little learning by the user. The first "calculator" to use RPN was the HP 9100 A, which was introduced in 1968 Price: 4900$ CIS 068

Evaluation Algorithm • Evaluate Expression from left to right • If symbol read is

Evaluation Algorithm • Evaluate Expression from left to right • If symbol read is operand: push onto the stack • If symbol read is operator: • pop two operators • Evaluate using operator • Push result on stack • Final value on stack is final result • (remark: works only on binary operators) CIS 068

Evaluation Algorithm Example CIS 068

Evaluation Algorithm Example CIS 068