CS 171: Introduction to Computer Science II Stacks and Queues Li - - PowerPoint PPT Presentation

cs 171 introduction to computer science ii stacks and
SMART_READER_LITE
LIVE PREVIEW

CS 171: Introduction to Computer Science II Stacks and Queues Li - - PowerPoint PPT Presentation

CS 171: Introduction to Computer Science II Stacks and Queues Li Xiong Roadmap Java basics OO and inheritance Arrays and ArrayList Abstract data types Abstract data types Stacks Queues Algorithm analysis Abstract


slide-1
SLIDE 1

CS 171: Introduction to Computer Science II Stacks and Queues

Li Xiong

slide-2
SLIDE 2

Roadmap

Java basics OO and inheritance Arrays and ArrayList Abstract data types Abstract data types

Stacks Queues

Algorithm analysis

slide-3
SLIDE 3

Abstract Data Types

Data type

A data type is a set of values and a set of

  • perations on those values

Abstract data type (ADT) Abstract data type (ADT)

An abstract data type is a data type whose internal representation is hidden from the client

slide-4
SLIDE 4

Implementing and using ADTs

ADTs are implemented in Java as classes

Instance variables are private (hidden from the client) Instance methods may be public (specified in the API)

  • r private (organize the computation and hidden from

the client)

Application programming interface (API)

specifies the behavior of an ADT including constructors and instance methods

Using ADTs

Create objects of implemented ADTs Invoke instance methods to operate on data-type values

slide-5
SLIDE 5

Examples of Java ADTs

Standard system ADTs

Integer, Double, String, StringBuilder …

Data oriented ADTs – facilitate organizing and processing data processing data

Point2D, Interval1D, Date, …

Collection ADTs – facilitate organizing and manipulating collections of data

Stack, Queue, Priority Queue, …

slide-6
SLIDE 6

Collection Abstract Data Types

Stacks and queues API - intended behavior Implementation – how to implement Implementation – how to implement Applications - how to use

slide-7
SLIDE 7
slide-8
SLIDE 8

Stacks

A stack stores an array of elements but with

  • nly two main operations:

Push: add an element to the top of the stack Pop: remove the top element of the stack. Pop: remove the top element of the stack.

Pop always removes the last element that’s added to the stack. This is called LIFO (Last- In-First-Out).

slide-9
SLIDE 9

Stacks – A Familiar Example

A can of tennis balls

Imagine the entire can represents an array, and each ball is an element. It only allows access to one element at a time: the last element. the last element. If you remove the last element, you can then access the next-to-last element. There is no way to directly access the element at the bottom.

slide-10
SLIDE 10

Stacks – Another Example

A dynamic list of tasks you perform everyday:

Imagine you start your day by working on task A. At any time you may be interrupted by a co- worker asking you for temporary help on task B. While you work on B, someone may interrupt While you work on B, someone may interrupt you again for help on task C. When you are done with C, you will resume working on B. Then you go back to work on A. Think about the sequence of tasks you perform.

slide-11
SLIDE 11

Stacks – Any other examples?

slide-12
SLIDE 12

Stack Examples

slide-13
SLIDE 13

Stacks

An element cannot be inserted to or accessed from the middle of the array. The only way you modify the elements is through the push and pop operations. This capability turns out to be very useful in many programming situations. In a computer, the stack is an essential data structure for handling program calls and returns.

slide-14
SLIDE 14

Stacks

Programmer’s tool

Arrays are typically used as data storage structures in apps such as a database (e.g. personal records, inventories …) In contrast, stacks are often used as In contrast, stacks are often used as programmer’s tool, and are not typically used for data storage.

slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21

Stack: Array implementation

Underflow: what happens if pop from an empty stack?

Throw exception

Overflow: what happens if push to a full Overflow: what happens if push to a full stack?

Use resizing array

slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24