Welcome to CIS 068 ! - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Welcome to CIS 068 !

Description:

Welcome to CIS 068 ! Lesson 11: Data Structures 2 Overview Description, Usage and Application of Queues and Stacks Overview Description, Usage and Application of ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 26
Provided by: RolfLak2
Learn more at: https://cis.temple.edu
Category:
Tags: cis | queues | stacks | welcome

less

Transcript and Presenter's Notes

Title: Welcome to CIS 068 !


1
Welcome to CIS 068 !
Lesson 11 Data Structures 2
2
Overview
  • Description, Usage and Application of
  • Queues and Stacks

3
Queues
  • FIFO (first in first out) Structure

in
3
2
1
out
4
Queues
  • Java Interface

5
Queues Implementation 1
  • 1. Implementation as Linked List

in
out
1
2
3
(null)
6
Adding an Element
in
out
in
out
in
out
2
(null)
1
1
(null)
(null)
  • Adding an element E
  • If in null in E, out E, E.link null
  • else in.link E, in E, E.link null

7
Removing an Element
in
out
in
out
in
out
2
1
2
(null)
(null)
(null)
  • Removing an element
  • If out null throw EmptyQueueException
  • else
  • Eout, outout.link
  • If out null in null
  • return E

8
Queues Implementation 2
2. Implementation as Circular Array
Capacity 6
Index !
3
4
5
in
---
out
1
2
size 5
List-Size 5
9
Circular Array Adding an Element E
in
out
out
9
9
10
10
11
11
12
12
13
13
E14
in
14
---
size 6
size 5
If size capacity return error (or increase
capacity automatically) else arrayinE size
in (in1)capacity
10
Circular Array Removing an Element
out
---
9
out
10
10
11
11
12
12
13
13
in
in
---
---
size 4
size 5
If size 0 throw EmptyQueueException else E
arrayout size -- out (out1)capacity
11
Stacks
  • LIFO (first in first out) Structure

top
3
2
1
12
Stacks
Java Interface
13
Stack Implementation 1
  • 1. Implementation as Linked List

top
3
2
1
(null)
14
Adding an Element E
2
1
top
(null)
2
1
top
E3
(null)
Adding an Element E E.link top top E
15
Removing an Element
2
1
top
3
(null)
2
1
top
(null)
return 3
Removing an Element E if topnull throw
EmptyQueueException else Etop top
top.link return E
16
Stack Implementation 2
  • 2. Implementation as Array

Capacity 6
Index !
---
---
top
4
3
2
1
17
Adding / Removing
---
---
top
4
3
2
1
Adding an Element E If top capacity return
error (or increase capacity automatically) else
arraytop E top
Removing an Element If top 0 throw
EmptyQueueException else top -- return
arraytop
18
Application 1
  • Checking for Balanced Parantheses
  • Task
  • Determine whether expression is balanced with
    respect to parantheses
  • Allow for different symbols of parentheses
  • ( )

19
Algorithm
20
Algorithm
  • Expression
  • (ab-cd(ab))
  • Stack
  • (
  • (
  • (
  • (
  • ((
  • (

Error !
21
Application 2
Evaluating a Postfix Expression Postfix
expression Why ? No brackets necessary !
22
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.

23
HP9100A
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 HP9100A, which
was introduced in 1968 Price 4900
24
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)

25
Evaluation Algorithm
Example
Write a Comment
User Comments (0)
About PowerShow.com