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.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Data Structures Static and Dynamic.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CMPT 225 Stacks.
Stacks and Queues.  Abstract Data Types  Stacks  Queues  Priority Queues September 2004John Edgar2.
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?
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
Data Structures and Algorithms
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.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
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.
Data structures Abstract data types Java classes for Data structures and ADTs.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
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.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
Cinda Heeren / Geoffrey Tien
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Principles of Computing – UFCFA3-30-1
Data Structures and Database Applications Stacks in C#
More Data Structures (Part 1)
Tonga Institute of Higher Education
Array Based Collections
ITEC 2620M Introduction to Data Structures
Stack Implementations
Chapter 5 Stack (part 1).
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
Presentation transcript:

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 called the bottom Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Using a Stack

What Are Stacks Used For? Most programming languages use a “call stack” to implement function calling When a method is called, its line number and other useful information are pushed (inserted) on the call stack When a method ends, it is popped (removed) from the call stack and execution restarts at the indicated line number in the method that is now at the top of the stack

The Call Stack This is a display of the call stack (from the Eclipse Debug window) Top of the stack: most recently called method. Bottom of the stack: least recently called method

Call Stacks and Recursion A call stack is what makes recursion possible Stacks are also important when traversing tree data structures They enable us to “backtrack” through the tree We’ll see more about this later in the course

Stack Operations A stack should implement (at least) these operations: push – insert an item at the top of the stack pop – remove and return the top item peek – return the top item (without removing it) These operations should be performed efficiently – in O(1) time

Axioms Axioms are used to define an ADT formally Example Axiom to specify that the last item inserted into stack is the first item to be removed (stack.push(newItem)).pop() = stack (stack.push(newItem)).peek() = newItem

Stack Implementation The stack ADT can be implemented using a variety of data structures. We will look at two: Arrays Linked Lists Both implementations must implement all the stack operations

Stack: Array Implementation If an array is used to implement a stack what is a good index for the top item? Is it position 0? Is it position n-1? Note that push and pop must both work in O(1) time as stacks are usually assumed to be extremely fast The index of the top of the stack is the number of items in the stack - 1

Array Stack Example index of top is current size – 1 //Java Code Stack st = new Stack(); st.push(6); //top = 0 st.push(1); //top = 1 st.push(7); //top = 2 st.push(8); //top = 3

Array Stack Example index of top is current size – 1 //Java Code Stack st = new Stack(); st.push(6); //top = 0 st.push(1); //top = 1 st.push(7); //top = 2 st.push(8); //top = 3 st.pop(); //top = 2

Array Implementation Summary Easy to implement a stack with an array push and pop can be performed in O(1) time But the implementation is subject to the limitation of arrays that the size must be initially specified because The array size must be known when the array is created and is fixed, so that the right amount of memory can be reserved Once the array is full no new items can be inserted If the maximum size of the stack is not known (or is much larger than the expected size) a dynamic array can be used But occasionally push will take O(n) time