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.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.
Chapter 3 Stacks.
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
Topic 15 Implementing and Using Stacks
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 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,
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,
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
TCSS 342, Winter 2005 Lecture Notes
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Topic 3 The Stack ADT.
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CHP-3 STACKS.
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.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Stacks Chapter 5.
Stacks Chapter 6.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
MEMORY REPRESENTATION OF STACKS
Objectives In this lesson, you will learn to: Define stacks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Chapter 5 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
Chapter 7 Stack.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

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 To understand how Java implements a stack To learn how to implement a stack using an underlying array or a linked list To see how to use a stack to perform various applications, including finding palindromes, testing for balanced (properly nested) parentheses, and evaluating arithmetic expressions

Chapter 5: Stacks3 Stack Abstract Data Type A stack can be compared to a Pez dispenser Only the top item can be accessed Can only extract one item at a time A stack is a data structure with the property that only the top element of the stack is accessible The stack’s storage policy is Last-In, First-Out

Chapter 5: Stacks4 Specification of the Stack Abstract Data Type Only the top element of a stack is visible, therefore the number of operations performed by a stack are few Need the ability to Inspect the top element Retrieve the top element Push a new element on the stack Test for an empty stack

Chapter 5: Stacks5 Specification of the Stack Abstract Data Type (continued)

Chapter 5: Stacks6 Stack Applications Two client programs using stacks Palindrome finder Parentheses matcher Palindrome: string that reads the same in either direction Example: “Able was I ere I saw Elba”

Chapter 5: Stacks7 Stack Applications (continued)

Chapter 5: Stacks8 Stack Applications (continued) When analyzing arithmetic expressions, it is important to determine whether an expression is balanced with respect to parentheses (a+b*(c/(d-e)))+(d/e) Problem is further complicated if braces or brackets are used in conjunction with parenthesis Solution is to use stacks!

Chapter 5: Stacks9 Stack Applications (continued)

Chapter 5: Stacks10 Stack Applications (continued)

Chapter 5: Stacks11 Implementing a Stack as an Extension of Vector The Java API includes a Stack class as part of the package java.util The vector class implements a growable array of objects Elements of a vector can be accessed using an integer index and the size can grow or shrink as needed to accommodate the adding and removing of elements

Chapter 5: Stacks12 Implementing a Stack as an Extension to Vector (continued)

Chapter 5: Stacks13 Implementing a Stack with a List Component Can use either the ArrayList, Vector, or the LinkedList classes as all implement the List interface Name of class illustrated in text is ListStack ListStack is an adapter class as it adapts the methods available in another class to the interface its clients expect by giving different names to essentially the same operations

Chapter 5: Stacks14 Implementing a Stack Using an Array Need to allocate storage for an array with an initial default capacity when creating a new stack object Need to keep track of the top of the stack No size method

Chapter 5: Stacks15 Implementing a Stack Using an Array (continued)

Chapter 5: Stacks16 Implementing a Stack as a Linked Data Structure We can implement a stack using a linked list of nodes

Chapter 5: Stacks17 Comparison of Stack Implementations Extending a Vector (as is done by Java) is a poor choice for stack implementation as all Vector methods are accessible Easiest implementation would be to use an ArrayList component for storing data All insertions and deletions are constant time regardless of the type of implementation discussed All insertions and deletions occur at one end

Chapter 5: Stacks18 Additional Stack Applications Consider two case studies that relate to evaluating arithmetic expressions Postfix and infix notation Expressions normally written in infix form Binary operators inserted between their operands A computer normally scans an expression string in the order that it is input; easier to evaluate an expression in postfix form

Chapter 5: Stacks19 Additional Stack Applications (continued)

Chapter 5: Stacks20 Additional Stack Applications (continued) Advantage of postfix form is that there is no need to group subexpressions in parentheses No need to consider operator precedence

Chapter 5: Stacks21 Evaluating Postfix Expressions

Chapter 5: Stacks22 Evaluating Postfix Expressions (continued)

Chapter 5: Stacks23 Evaluating Postfix Expressions (continued)

Chapter 5: Stacks24 Evaluating Postfix Expressions (continued)

Chapter 5: Stacks25 Converting from Infix to Postfix

Chapter 5: Stacks26 Additional Stack Applications (continued)

Chapter 5: Stacks27 Evaluating Postfix Expressions (continued)

Chapter 5: Stacks28 Evaluating Postfix Expressions (continued)

Chapter 5: Stacks29 Chapter Review A stack is a last-in, first-out (LIFO) data structure A stack is a simple but powerful data structure; its four operations include empty, peek, pop, and push Stacks are useful to process information in the reverse of the order that it is encountered Java.util.Stack is implemented as an extension of the Vector class

Chapter 5: Stacks30 Chapter Review (continued) Three ways to implement a stack: Using an object of a class that implements the List interface as a container Using an array as a container Using a linked list as a container Stacks can be applied in programs for evaluating arithmetic expressions