week7 stack data structures & algorithms. introduction to stacks and queues widely used data...

16
Week7 Stack Data Structures & Algorithms

Upload: claud-gilbert

Post on 11-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Week7 Stack

Data Structures &Algorithms

Page 2: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Introduction to

Stacks and Queues

• Widely used data structures

• Ordered List of element

• Easy to implement

• Easy to use

63

Page 3: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Stacks

S=(a0,...,an 1)

Insert Delete

 a0 is the bottom of the stack

an 1is the top of the stack

Top

Bottom

a3

a2

a1

a0

Insertions and deletions are made at the topLast In First Out (LIFO) list

Example: stack of plates

64

Page 4: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

The Stack

Page 5: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Describe the output of the following series of stack operations

Push(8)Push(3)Pop()Push(2)Push(5)Pop()Pop()Push(9)Push(1)

Page 6: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Elementary Data Structures 6

The Stack Operation

Insertions and deletions follow the last-in first-out (LIFO) scheme

Main stack operations:• push(Object o): inserts

element o• pop(): removes and returns

the last inserted element

Auxiliary stack operations:

• top(): returns the last inserted element without removing it

• size(): returns the number of elements stored

• isEmpty(): a Boolean value indicating whether no elements are stored

– isFull() (a Boolean value indicating whether a stack is full or not)

Page 7: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Additional Notes

Stacks structures are usually implemented using arrays or linked lists.

For both implementations, the running time is O(n).

We will be examining common Stack Applications.

Page 8: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Stack Applications

Reversing Data: We can use stacks to reverse data.(example: files, strings)Very useful for finding palindromes.

Consider the following pseudocode:1) read (data)2) loop (data not EOF and stack not full)

1) push (data)2) read (data)

3) Loop (while stack notEmpty)1) pop (data)2) print (data)

Page 9: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Stack Applications

Postponement: Evaluating arithmetic expressions.

Prefix: + a b Infix: a + b (what we use in grammar school) Postfix: a b +

In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the expression to determine the order in which we evaluate it. A common technique is to convert a infix notation into postfix notation, then evaluating it.

Page 10: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Infix to postfix

Change the following expression from infix to postfix :5*(((9+8)*(4*6))+7)

Page 11: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

evaluate the following expression:598+46**7*+

Page 12: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Exercise: Change the following expression from infix to postfix:

)A * B) +( C - D / E (

Page 13: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Stack implementation in c

Page 14: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Stack implementation in c

Page 15: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

Stack implementation in c

Page 16: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy

END