Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Advertisements

Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
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?
Stacks.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
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.
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. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stacks and Queues Introduction to Computing Science and Programming I.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures & Algorithms
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Stacks II David Lillis School of Computer Science and Informatics
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Stacks.
" A list is only as strong as its weakest link. " - Donald Knuth
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
CS210- Lecture 3 Jun 6, 2005 Announcements
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3

 Definition of Stacks  Operators for Stacks  Push  Pop  Peek  Applications for Stacks  Reversing a Word  Delimiter Matching  Array-based Stack

 A stack is a Last-In-First-Out(LIFO) abstract data structure.  In a stack, access to a element is restricted: only the top item can be read or removed at one time Top Bottom

Fig1. Bullets Clip: A Typical Stack

 You can only eat the top apple first Fig.2 An Apple Stack

 Push: insert a data item to the top of the stack Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Fig2. Push Operator in Stacks Bottom Top

Program  Pop: get a data item on the top of the stack and remove it from the stack Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Fig2. Pop Operator in Stacks Bottom Top

Program  Peek: get a data item on the top of the stack and do not remove it from the stack Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Fig2. Pop Operator in Stacks Bottom Item 6 Top

 Stacks are typically small, temporal data structures.  Can be calculated by the top pointer and the bottom pointer

Output Input  Reversing a Word  “Avatar” -> “ratavA” A A v v a a t t a a r r BottomTop

11  In processing programs and working with computer languages there are many instances when symbols must be balanced { }, [ ], ( ) A stack is useful for checking symbol balance. When a closing symbol is found it must match the most recent opening symbol of the same type. Algorithm?

12  Make an empty stack  read symbols until end of file  if the symbol is an opening symbol push it onto the stack  if it is a closing symbol do the following ▪ pop the stack. If the symbol popped does not match the closing symbol report an error  At the end of the file if the stack is not empty report an error

 Delimiter Matching  ( ) * ( ) Stack: 9 ( + 1 ) (9+1)=10 10 * ( ) (6+2)=8 8 10*8=80 80 = 80

 Items can be both pushed and popped from a stack in constant O(1) time  We have no choices but just operate on the particular item (top item)  Very Quick

 How is stack implemented?  Initiate an array  Use variables to indentify the position of the top of the stack A[10]top=-1 Pop() Push(1) Push(8) Push(3) Pop() 3 top=0 1 top=1 8

Elementary Data Structures16 A simple way of implementing the Stack uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t … Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t  t  1 return S[t + 1]

Elementary Data Structures17 The array storing the stack elements may become full A push operation will then throw a FullStackException  Limitation of the array- based implementation  Not intrinsic to the Stack S 012 t … Algorithm push(o) if t = S.length  1 then throw FullStackException else t  t + 1 S[t]  o

Elementary Data Structures18 In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push(o) if t = S.length  1 then A  new array of size … for i  0 to t do A[i]  S[i] S  A t  t + 1 S[t]  o

 Definition of Stacks  Operators for Stacks  Push  Pop  Peek  Applications for Stacks  Reversing a Word  Delimiter Matching  Array-based Stack