1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.

Slides:



Advertisements
Similar presentations
Chapter 1: INTRODUCTION TO DATA STRUCTURE
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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 CSE, POSTECH 2 2 Queues Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue)
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.
Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top.
Definition: Domain The domain of a function is the set of all x-values. Since the x-values run from left to right in the coordinate plane, we read the.
Topic 15 Implementing and Using Stacks
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CS Data Structures Chapter 3 Stacks and Queues.
Modern VLSI Design 2e: Chapter 6 Copyright  1998 Prentice Hall PTR Topics n Shifters. n Adders and ALUs.
Summary of lectures (1 to 11)
Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using 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.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,
Data Structures and Abstractions with Java, 4e Frank Carrano
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)
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
SNU IDB Lab. 1 Ch9. Stacks © copyright 2006 SNU IDB Lab.
CSC 205 Programming II The ADT Stack. Recap: ADT Abstract Data Type A collection of data (objects) A set of operations on that data Add Remove Retrieve.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
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.
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.
1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem.
COP 3530 Discussion Session #6 Yilin Shen. Outline Chapter 7 o Q35p 250 o Q39p 265 Chapter 8 o Problem 8.5.3: Rearranging Railroad carsp 289 o Problem.
Midterm Review Linear list Stack queue. What is a data structure What is an abstract data type.
COP-3530 Data Structures and Algorithms Midterm I.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Click to edit Master text styles Stacks Data Structure.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Programming Application in Civil Engineering
COP-3530 Data Structures and Algorithms Midterm I
Stacks Stacks.
Stacks Linear list. One end is called top. Other end is called bottom.
Objectives In this lesson, you will learn to: Define stacks
Stack and Queue.
Stacks, Queues, and Deques
Fundamentals of Programming
Indirection.
Topic 15 Implementing and Using Stacks
Program to find equivalence classes
Queues Linear list. One end is called front. Other end is called rear.
Stack Implementations
Presentation transcript:

1 Chapter 5 Stacks

2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses matching –tower of Hanoi –Re-arranging Railroad cars –computer-aided design of circuit field –offline equivalence class –rat in a maze

3 Stacks

4 Abstract data type

5 Formula-based representation

6 Formula-based representation (continue)

7 Efficiency of Stack Constructor and destructor –Θ(1) when T is an internal data type –Θ(MaxStackSize) when T is user-defined all other operations - Θ(1)

8 Customized definition of Stack

9 Customized definition of Stack (continue)

10 Customized definition of Stack (continue)

11 Two stacks in an array Multiple stacks in an array –Worst-case add time - O(ArraySize) –delete time - Θ

12 Linked Stack

13 Linked Stack (continue)

14 Customized linked stack each operation takes Θ(1) time

15 Customized linked stack (continue)

16 Customized linked stack (continue)

17 Customized linked stack (continue)

18 Customized linked stack (continue)

19 Parenthesis matching - Θ(n)

20 Parenthesis matching (continue)

21 Parenthesis matching (continue)

22 Sample run

23 Towers of Hanoi

24 Recursive function

25 Time complexity moves(n) = –0 when n = 0 –2moves(n-1)+1 when n > 0 moves(n) = 2 n - 1 Θ(2 n )

26 Towers of Hanoi using stacks

27 Towers of Hanoi using stacks (continue)

28 Towers of Hanoi using stacks (continue)

29 Rearranging railroad cars

30 Rules A car may be moved from the front of the input track to the top of one of the holding tracks or to the left end of the output track A car may be moved from the top of a holding track to the left end of the output track whenever the car labels in a holding track are not in increasing order from top to bottom, the rearrangement cannot be completed new car u is moved to the holding track that has at its top a car with smallest label v such that v > u

31 Track states

32 Railroad car rearrangement program - O(kn)

33 Railroad car rearrangement program (continue)

34 Railroad car rearrangement program (continue)

35 Output - Θ(k)

36 Output (continue)

37 Hold - Θ(k)

38 Hold (continue)

39 Hold (continue)

40 Switch box routing

41 The routing program - Θ(n)

42 The routing program (continue)

43 Rat in a Maze

44 Matrix Rep

45 The end of Chapter 5