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
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
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)
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
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. 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.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
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 Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
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.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
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.
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.
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)
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
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.
 STACK STACK  STACK OPERATIONS STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  USES OF STACK USES OF STACK  THE TOWER.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
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.
Infix to postfix conversion
Stacks Linear list. One end is called top. Other end is called bottom.
Objectives In this lesson, you will learn to: Define stacks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACKS.
More Recursion.
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.
پشته ها و صف ها.
Stacks.
Stack.
More About Stacks: Stack Applications
Queues Linear list. One end is called front. Other end is called rear.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
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

The Abstract data type of Stack

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

Class Stack

Class Stack – cont.

Towers Of Hanoi 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 3-disk Towers Of Hanoi A BC 1 2 3

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

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

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

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

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

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

Towers Of Hanoi 3-disk Towers Of Hanoi 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 moves(64) = 1.8 * (approximately) Performing 10 9 moves/second, a computer would take about 570 years to complete.

Towers Of Hanoi

Tower of Hanoi using Stacks

Stack -- using Linked List

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.