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

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

The unorganized person’s data structure
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.
Chapter 3 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 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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
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.
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.
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.
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
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.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
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.
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.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Click to edit Master text styles Stacks Data Structure.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
COSC160: Data Structures: Lists and Queues
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
Stacks.
Cinda Heeren / Geoffrey Tien
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
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.
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 CS-240 Dick Steflik.
Stacks.
Stacks.
Abstract Data Types Stacks CSCI 240
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

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 CSCI 3333 Data Structures10