Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

Slides:



Advertisements
Similar presentations
Stacks Chapter 11.
Advertisements

Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
Infix, Postfix, Prefix.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Data Structures & Algorithms
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO.
CHP-3 STACKS.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.
Programming Application in Civil Engineering
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Stacks Stacks.
Stacks 황승원 Fall 2010 CSE, POSTECH.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Linear list. One end is called top. Other end is called bottom.
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACKS.
Stacks Chapter 4.
Stack application: postponing data usage
Java Software Structures: John Lewis & Joseph Chase
Stack and Queue.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Queues Linear list. One end is called front. Other end is called rear.
پشته ها و صف ها.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Queues Linear list. One end is called front. Other end is called rear.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

Stack Of Cups Add a cup to the stack. bottom top C A B D E F Remove a cup from new stack. A stack is a LIFO list. bottom top C A B D E

Parentheses Matching (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) –Output pairs (u,v) such that the left parenthesis at position u is matched with the right parenthesis at v. (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38) (a+b))*((c+d) –(0,4) –right parenthesis at 5 has no matching left parenthesis –(8,12) –left parenthesis at 7 has no matching right parenthesis

Parentheses Matching scan expression from left to right when a left parenthesis is encountered, add its position to the stack when a right parenthesis is encountered, remove matching position from stack

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 2

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13) 15

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19) 21

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25) 27

Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25)(27,31)(0,32) and so on

Towers Of Hanoi/Brahma ABC gold disks to be moved from tower A to tower C each tower operates as a stack cannot place big disk on top of a smaller one

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC disk moves

Recursive Solution ABC 1 n > 0 gold disks to be moved from A to C using B move top n-1 disks from A to B using C

Recursive Solution A BC 1 move top disk from A to C

Recursive Solution A BC 1 move top n-1 disks from B to C using A

Recursive Solution A BC 1 moves(n) = 0 when n = 0 moves(n) = 2*moves(n-1) + 1 = 2 n -1 when n > 0

Towers Of Hanoi/Brahma moves(64) = 1.8 * (approximately) Performing 10 9 moves/second, a computer would take about 570 years to complete. At 1 disk move/min, the monks will take about 3.4 * years.

Chess Story 1 grain of rice on the first square, 2 for next, 4 for next, 8 for next, and so on. Surface area needed exceeds surface area of earth.

Chess Story 1 penny for the first square, 2 for next, 4 for next, 8 for next, and so on. $3.6 * (federal budget ~ $2 * ).

Switch Box Routing Routing region

17 Routing A 2-pin Net Routing for pins 5 through 16 is confined to upper right region. Routing for pins 1-3 and is confined to lower left region.

17 Routing A 2-pin Net (u,v), u<v is a 2-pin net. u is start pin. v is end pin. Examine pins in clock- wise order beginn- ing with pin 1.

17 Routing A 2-pin Net Start pin => push onto stack. End pin => start pin must be at top of stack.

Method Invocation And Return public void a() { …; b(); …} public void b() { …; c(); …} public void c() { …; d(); …} public void d() { …; e(); …} public void e() { …; c(); …} return address in a() return address in b() return address in c() return address in d() return address in e() return address in c() return address in d()

Try-Throw-Catch When you enter a try block, push the address of this block on a stack. When an exception is thrown, pop the try block that is at the top of the stack (if the stack is empty, terminate). If the popped try block has no matching catch block, go back to the preceding step. If the popped try block has a matching catch block, execute the matching catch block.

Rat In A Maze

Move order is: right, down, left, up Block positions to avoid revisit.

Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.

Rat In A Maze Move backward until we reach a square from which a forward move is possible.

Rat In A Maze Move down.

Rat In A Maze Move left.

Rat In A Maze Move down.

Rat In A Maze Move backward until we reach a square from which a forward move is possible.

Rat In A Maze Move backward until we reach a square from which a forward move is possible. Move downward.

Rat In A Maze Move right. Backtrack.

Rat In A Maze Move downward.

Rat In A Maze Move right.

Rat In A Maze Move one down and then right.

Rat In A Maze Move one up and then right.

Rat In A Maze Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.