welcome to cis 068 !

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

Upload: callum

Post on 05-Jan-2016

50 views

Category:

Documents


0 download

DESCRIPTION

Welcome to CIS 068 !. Lesson 11: Data Structures 2. Overview. Description, Usage and Application of Queues and Stacks. Queues. FIFO (first in first out) Structure. in. 3. 2. 1. out. Queues. Java Interface. Queues: Implementation 1. 1. Implementation as Linked List. in. out. 3. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Welcome to CIS 068 !

CIS 068

Welcome to CIS 068 !Lesson 11:

Data Structures 2

Page 2: Welcome to CIS 068 !

CIS 068

Overview

Description, Usage and Application of

Queues and Stacks

Page 3: Welcome to CIS 068 !

CIS 068

Queues• FIFO (first in first out) Structure

1

out

2

3

in

Page 4: Welcome to CIS 068 !

CIS 068

Queues• Java Interface

Page 5: Welcome to CIS 068 !

CIS 068

Queues: Implementation 1

1. Implementation as Linked List

123

in out

(null)

Page 6: Welcome to CIS 068 !

CIS 068

Adding an Element

Adding an element E:

• If in == null: in = E, out = E, E.link = null

• else in.link = E, in = E, E.link = null

2

in out

(null) 1

in out

(null) 1

in out

(null)

Page 7: Welcome to CIS 068 !

CIS 068

Removing an Element

Removing an element:

• If out == null throw EmptyQueueException

• else

• E=out, out=out.link

• If out = null: in = null

• return E

2 1

in out

(null) 2

in out

(null)

in out

(null)

Page 8: Welcome to CIS 068 !

CIS 068

Queues: Implementation 22. Implementation as Circular Array

1in

out2

---543

size = 5

Capacity = 6

List-Size = 5

Index !

Page 9: Welcome to CIS 068 !

CIS 068

Circular Array: Adding an Element E

13in

out

---

1211109

size = 5

If size == capacity

return error (or increase capacity automatically)

else

array[in]=E

size++

in = (in+1)%capacity

13

inout

14

1211109

size = 6

E=14

Page 10: Welcome to CIS 068 !

CIS 068

Circular Array: Removing an Element

13in

out

---

1211109

size = 5

If size == 0

throw EmptyQueueException

else

E = array[out]

size --

out = (out+1)%capacity

13in

out

---

121110---

size = 4

Page 11: Welcome to CIS 068 !

CIS 068

Stacks• LIFO (first in first out) Structure

1

2

3

top

Page 12: Welcome to CIS 068 !

CIS 068

Stacks

Java Interface

Page 13: Welcome to CIS 068 !

CIS 068

Stack: Implementation 1

1. Implementation as Linked List

321

top

(null)

Page 14: Welcome to CIS 068 !

CIS 068

Adding an Element E

Adding an Element E:

E.link = top

top = E

21 top(null)

21 top(null) E=3

Page 15: Welcome to CIS 068 !

CIS 068

Removing an Element

Removing an Element E:

if top==null

throw EmptyQueueException

else

E=top

top = top.link

return E

21 top(null)

21 top(null) 3

return 3

Page 16: Welcome to CIS 068 !

CIS 068

Stack: Implementation 2

2. Implementation as Array

2

top

1

34------

Capacity = 6

Index !

Page 17: Welcome to CIS 068 !

CIS 068

Adding / Removing

2

top

1

34------

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 EmptyQueueException

else

top --

return array[top]

Page 18: Welcome to CIS 068 !

CIS 068

Application 1Checking for Balanced Parantheses

Task:

• Determine whether expression is balanced with respect to parantheses

• Allow for different symbols of parentheses:

• ( )

• [ ]

• { }

Page 19: Welcome to CIS 068 !

CIS 068

Algorithm

Page 20: Welcome to CIS 068 !

CIS 068

Algorithm

Expression:

(a+{b-c}*[d+(a+b))

Stack:• (

• ({

• (

• ([

• ([(

• ([

Error !

Page 21: Welcome to CIS 068 !

CIS 068

Application 2Evaluating a Postfix Expression

Postfix expression:

Why ? No brackets necessary !

Page 22: Welcome to CIS 068 !

CIS 068

PN / RPNPrefix / 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.

Page 23: Welcome to CIS 068 !

CIS 068

HP9100AEngineers 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 HP9100A, which was introduced in 1968

Price: 4900$

Page 24: Welcome to CIS 068 !

CIS 068

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)

Page 25: Welcome to CIS 068 !

CIS 068

Evaluation AlgorithmExample