Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
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.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
COMP 110 Introduction to Programming Mr. Joshua Stough.
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.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
© 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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
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.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
MEMORY REPRESENTATION OF STACKS
Stacks.
Cinda Heeren / Geoffrey Tien
Week 15 – Monday CS221.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
i206: Lecture 11: Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture 21 Stacks and Queues Richard Gesick.
Stacks.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks.
CE 221 Data Structures and Algorithms
Stacks.
Presentation transcript:

Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures

Outline Intro. to Stacks java.util.Stack Example programs CSCI 3333 Data Structures2

3 Stacks A data structure in which access is restricted to the most recently inserted item. Supported operations: – push( n): add n to the top of the stack – pop( ): remove the object at the top of the stack and return that object – top( ): aka. peek( ) return the object at the top of the stack All three operations take constant time.

Stacks Special features: Last in, first out (LIFO) First in, last out (FILO) top( ) and pop( ) returns the most recently pushed item in the stack. CSCI 3333 Data Structures4

Applications of Stack In compiler design: – Evaluation of balanced expressions e = 3 * (5 – (8 + 2) * 4)+ 8 / (4 + 2) Q: How would a Stack help to evaluate this expression? See 11.1 balanced symbol checker & 11.2 a simple calculator – Evaluation of function/method calls e.g., main()  f1( )  f2( ) and f3( ), etc. A function tree (next). CSCI 3333 Data Structures5

Function tree Q: How would a stack be used to manage the function calls? Q: How would the local variables and their values be managed across functions? Q: recursive functions ? CSCI 3333 Data Structures6

Evaluation of a recursive function call CSCI 3333 Data Structures7 Q: What is F(5)? F(10)? Note: Fibonacci is known to deliver poor performance when implemented as a recursive function.

Evaluation of a recursive function call CSCI 3333 Data Structures8 Revisit the evaluation of maxSumRec( ): private static int maxSumRec( int [ ] a, int left, int right ) int [ ] a = {-10,20,30,-40,50,60}; The call: maxSumRec(a, 0, 5)  maxSumRec(a,0,2), maxSumRec(a,3,5)  … Q: How would a stack be used to maintain all the variables and returned values?

Implementation of Stack A Stack may be implemented as an Array or a Linked List. Stack as a dynamic array: – ArrayStack.java (Figure 16.2 – 16.7) Private [ ] theArray; private int topOfStack; Stack as a linked list: – ListStack.java (Figure – 16.21) private ListNode topOfStack; CSCI 3333 Data Structures9

Implementation of Stack Based on the ArrayStack.java (Figure 16.2 – 16.7) and related programs, complete the following exercises. 1.Run the ArrayStackDemo application (available at latedTopics.htm#stacks).ArrayStackDemo latedTopics.htm#stacks 2.Add an instance variable (and appropriate methods) to keep track of the size of the stack. Run the ListStack.java (Figure – 16.21) application. CSCI 3333 Data Structures10

java.util.Stack java.lang.Object -- java.util.AbstractCollection java.util.AbstractCollection -- java.util.AbstractList java.util.AbstractList -- java.util.Vector java.util.Vector -- java.util.Stack Sample applications using the Stack class ( dTopics.htm#stacks) dTopics.htm#stacks – StackDemo – StackPerson CSCI 3333 Data Structures11