Stacks II David Lillis School of Computer Science and Informatics

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

Stacks, Queues, and Linked Lists
Stacks, Queues, and Deques
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 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.
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.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Abstract Data Type (ADT) & Stacks
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
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.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
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.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
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,
Section 3.7 Linked-Based Implementations
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Review Array Array Elements Accessing array elements
Lists Rem Collier Room A1.02
Stacks.
Stacks and Queues Chapter 4.
CSCI 3333 Data Structures Stacks.
Queues Rem Collier Room A1.02
CC 215 Data Structures Stack ADT
Exceptions, Interfaces & Generics
Stacks.
Stacks and Queues.
Queues Queues Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack and Queue APURBO DATTA.
i206: Lecture 11: Stacks, Queues
CMSC 341 Lecture 5 Stacks, Queues
i206: Lecture 10: Lists, Stacks, Queues
Stacks.
Queues.
Stacks.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix.
Array Based Collections
ADT list.
CSE 214 – Computer Science II Stacks
Stacks: Implemented using Linked Lists
Stacks Abstract Data Types (ADTs) Stacks
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2001 Bin Sort, Radix.
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
CSCS-200 Data Structure and Algorithms
Abstract Data Types Stacks CSCI 240
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks II David Lillis School of Computer Science and Informatics University College Dublin, Ireland

Recap: Stacks Concept A stack is a container of objects / values. Insertion and removal based on the last-in-first-out (LIFO) principle. Objects can be inserted at any time, but only the last (the most-recently inserted) object can be removed. Terminology: Items are “Pushed” onto the stack (insertion). Items can be “Popped” off the stack (removal). The “Top” of the stack is the last item pushed A PEZ® dispenser as an analogy…

Recap: Functional Specification Stacks should work with objects. Core Operations: push(o): Inserts object o onto top of stack. pop(): Removes the top object of stack and returns it. Support Operations: size(): Returns the number of objects in stack. isEmpty(): Return a boolean indicating if stack is empty. top(): Return the top object of the stack, without removing it.

Recap: Implementation Strategies Array-based Implementation: Array holds the objects pushed onto the stack Insertion begins at index 0. Auxiliary value needed to keep track of the “top” of the stack. Finite Capacity Link-based Implementation: Objects stored in special “nodes” Nodes maintain ordering information Link to the next object in the stack. Infinite Capacity

Array-Based Stacks Array-based implementations => Finite Capacity. Memory (Mis-)management issues. Possible Solution: Use an “extendable array” Creating a new larger array and copying the existing values into it. Running time – O(n) => Pop / Push running times become O(n) Are there any other implementation strategies? YES! We can use the Linked List data structure.

Linked Lists In a Linked List, the objects are stored in nodes. Each node also maintains a reference to the next node in the list (this is the link). The link of the last node in the list is set to null. We also store references to “key” nodes / entry points. These provide us with a way of accessing the list element next “Rome” null means “end of list”

Link-Based Stack Auxiliary Data Structure: Node Key Nodes / Data: A reference to the object being stored in the stack (the "element") A link to the next node in the stack (the "link"). Key Nodes / Data: The “top” node of the stack The link of the bottom node in a non-empty stack is set to null. Need to keep track of the “size” of the stack top   top Ireland France Empty Stack Stack of size 2

Interlude: Inner Classes A class that is declared as part of another class. The inner class is in effect part of implementation of the outer class. Thus the outer class has full access to an inner class, even if fields/methods are marked as private. Why use one? Logically groups code that is only used in one place Increases readability and maintainability Example: public class LinkedStack { private class Node { … }

Link-Based Stacks: Dry Runs View operations as atomic Show the state of the Linked List after each operation Example: push(“France”) top  top  push(“Ireland”) France top  top Ireland France  push(“Scotland”) Scotland Ireland France

Link-Based Stack Algorithm push(o): Input: an object o Output: none node  new Node(o) node.next  top top  node size  size + 1 Algorithm size(): Input: none Output: count of objects on the stack return size Algorithm isEmpty(): Output: true if the stack is empty, false otherwise return size = 0 Algorithm pop(): Input: none Output: the top object e  top.element top.element  null top  top.next size  size-1 return e Algorithm top(): return top.element

Link-Based Stacks: Implementation Class name: LinkedStack Fields: An inner class Node An integer, size (number of objects in the stack) A Node field, top (references top node in stack) Constructors Default Constructor (sets top to null and size to 0) Methods: 1 per operation: methods names should match operation names Implement methods based on pseudo code

Link-Based Stacks: Analysis Operation Running Times: Issues: What happens if we pop from an empty stack? Which Implementation Strategy is better? Operation Running Time push(o) O(1) pop() top() isEmpty() size()