CS350: Data Structures Stacks

Similar documents
Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Data Structures And Algorithms

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stacks Fall 2018 Margaret Reid-Miller

CSCD 326 Data Structures I Stacks

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Stacks and Queues. David Greenstein Monta Vista

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CSC 273 Data Structures

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Stack Abstract Data Type

Interfaces & Generics

Stack and Its Implementation

CE 221 Data Structures and Algorithms

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Associate Professor Dr. Raed Ibraheem Hamed

CS24 Week 4 Lecture 2

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

CS350: Data Structures Linked Lists

CMPS 390 Data Structures

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS350: Data Structures. Doubly Linked Lists

CMSC 132: Object-Oriented Programming II. Stack and Queue

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CSC 1052 Algorithms & Data Structures II: Stacks

Topic 6: Inner Classes

CSE 143 SAMPLE MIDTERM

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

More Data Structures (Part 1) Stacks

Programming II (CS300)

1. Stack Implementation Using 1D Array

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Midterm Exam 2 CS 455, Spring 2011

Algorithms & Datastructures Laboratory Exercise Sheet 1

Stacks and their Applications

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

Basic Data Structures

An Introduction to Queues With Examples in C++

Queues Fall 2018 Margaret Reid-Miller

Abstract Data Types. Abstract Data Types

CSC 1052 Algorithms & Data Structures II: Queues

Basic Data Structures 1 / 24

Stacks, Queues (cont d)

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

Standard ADTs. Lecture 19 CS2110 Summer 2009

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CE204 Data Structures and Algorithms Part 2

CS350: Data Structures Binary Search Trees

What can we do with Coin dispenser?

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

Queues. Stacks and Queues

Stacks and Queues as Collections

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Priority Queues. 04/10/03 Lecture 22 1

Data Structures G5029

Announcements Queues. Queues

csci 210: Data Structures Stacks and Queues

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

CS 350 : Data Structures Binary Search Trees

Building Java Programs

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Exceptions and Design

Lecture No.04. Data Structures

CS 310: Array- and Linked-based Data Structures

Programming II (CS300)

CS350: Data Structures Red-Black Trees

Derive From A Linear List Class. Stacks. Derive From ArrayLinearList. Derive From ArrayLinearList. Derive From ArrayLinearList.

ECE Fall st Midterm Examination (120 Minutes, closed book)

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

COMP : Trees. COMP20012 Trees 219

CMSC 341 Lecture 7 Lists

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CS 211 Programming Practicum Spring 2017

Building Java Programs

Lists are great, but. Stacks 2

A queue is a linear collection whose elements are added on one end and removed from the other

CS350: Data Structures AA Trees

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

Lecture Data Structure Stack

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Data Structures and Algorithms

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

CSIS 10B Lab 2 Bags and Stacks

CMSC 341. Binary Heaps. Priority Queues

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

ITEC2620 Introduction to Data Structures

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CMPT 225. Lecture 9 Stack

Transcription:

Stacks James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola

Stacks Stacks are a very common data structure that can be used for a variety of data storage purposes Similar to a stack of papers sitting on a table... - Items can be placed on the stack of papers - You can take a look at whats on the top of your stack of papers - Items can be removed from the top of your stack of papers - If you want a paper in the middle of the stack, you must remove the papers on top to get to the paper in the middle May also be referred to as a LIFO (Last-In-First-Out) 2

Stack Operations Stacks have three main operations - push - puts an element on the top of the stack - pop - removes a single element from the top of the stack - top or peek - returns the value of the element on the top of the stack (without removing it) 3

Stack Operations Start with Empty Stack Top Bottom 4

Stack Operations Push Value: A Top A Bottom 5

Stack Operations Push Value: B Top B A Bottom 6

Stack Operations Push Value: C Top C B A Bottom 7

Stack Operations Pop the Top of the Stack: Top C B A Bottom 8

Stack Operations Pop the Top of the Stack: Top B A Bottom 9

A Stack Interface in Java public interface Stack<AnyType> { public void push( AnyType x ); public AnyType pop( ); public AnyType peek( ); public boolean isempty( ); } public void makeempty( ); 10

Stack Implementations Stacks can be implemented in multiple ways Two popular methods for implementing stacks include - (1) Arrays - (2) Linked Lists Both of these implementation approaches allow for constant time operations - O(1) 11

Stack Implementation Using an Array Start with Empty Stack (i.e. an array) first column is array index 4 3 2 top-of-stack pointer is initialized to -1 1 0 top-of-stack 12

Stack Implementation Using an Array Insert Value: A 4 3 2 1 top-of-stack 0 A 13

Stack Implementation Using an Array Insert Value: B 4 3 2 top-of-stack 1 0 B A 14

Stack Implementation Using an Array Insert Value: C 4 3 top-of-stack 2 1 0 C B A 15

Stack Implementation Using an Array Pop the Top of the Stack: 4 3 top-of-stack 2 1 0 C B A 16

Stack Implementation Using an Array Pop the Top of the Stack: 4 3 2 top-of-stack 1 B 0 A 17

Stack Implementation Using an Array Considerations when using an array implementation - push, pop, and peek operations all run in constant time... in most cases - What happens when your array is full and you want to push another element? Array must be increased in size which takes time an more memory Time to copy and create new array is O(N) Time to copy array is amortized over the lifetime of the array May not be suitable for all types of systems (e.g. RTOS) 18

Stack Implementation Using an Array (Java) /** * Stack constructor */ public ArrayStack( ) { thearray = (AnyType []) new Object[ DEFAULT_CAPACITY ]; topofstack = -1; } 19

Stack Implementation Using an Array (Java) /** * Test if the stack is logically empty. * @return true if empty, false otherwise. */ public boolean isempty( ) { return topofstack == -1; } 20

Stack Implementation Using an Array (Java) /** * Make the stack logically empty. */ public void makeempty( ) { topofstack = -1; } 21

Stack Implementation Using an Array (Java) /** * Insert a new item into the stack. * @param x the item to insert. */ public void push( AnyType x ) { if( topofstack + 1 == thearray.length ) doublearray( ); thearray[ ++topofstack ] = x; } 22

Stack Implementation Using an Array (Java) /** * Remove the most recently inserted item from the stack. * @throws UnderflowException if the stack is empty. */ public void pop( ) { if( isempty( ) ) throw new UnderflowException( "ArrayStack pop" ); topofstack--; } 23

Stack Implementation Using an Array (Java) /** * Get the most recently inserted item in the stack. * Does not alter the stack. * @return the most recently inserted item in the stack. * @throws UnderflowException if the stack is empty. */ public AnyType peek( ) { if( isempty( ) ) throw new UnderflowException( "ArrayStack top" ); return thearray[ topofstack ]; } 24

Stack Implementation Using an Array (Java) /** * Internal method to extend thearray. */ private void doublearray( ) { AnyType [ ] newarray; } newarray = (AnyType []) new Object[ thearray.length * 2 ]; for( int i = 0; i < thearray.length; i++ ) newarray[ i ] = thearray[ i ]; thearray = newarray; 25

Stack Implementation Using a LinkedList Start with Empty Stack (i.e. a null LinkedList) top-of-stack points to the head of the top-of-stack NULL 26

Stack Implementation Using a LinkedList Push Value: A top-of-stack A NULL 27

Stack Implementation Using a LinkedList Push Value: B top-of-stack B A NULL 28

Stack Implementation Using a LinkedList Push Value: C top-of-stack C B A NULL 29

Stack Implementation Using a LinkedList Pop the Top of the Stack: top-of-stack C B A NULL 30

Stack Implementation Using a LinkedList Pop the Top of the Stack: top-of-stack B A NULL 31

Stack Implementation Using a LinkedList Considerations when using a LinkedList implementation - push, pop, and peek operations all run in constant time... still - Each element inserted into the stack requires a pointer to the data and a second pointer to the next node in the LinkedList 32

Stack Implementation Using a LinkedList (Java) /** * Stack constructor */ public ListStack( ) { topofstack = null; } private ListNode<AnyType> topofstack; 33

Stack Implementation Using a LinkedList (Java) /** * Test if the stack is logically empty. * @return true if empty, false otherwise. */ public boolean isempty( ) { return topofstack == null; } 34

Stack Implementation Using a LinkedList (Java) /** * Make the stack logically empty. */ public void makeempty( ) { topofstack = null; } 35

Stack Implementation Using a LinkedList (Java) /** * Insert a new item into the stack. * @param x the item to insert. */ public void push( AnyType x ) { topofstack = new ListNode<AnyType>( x, topofstack ); } 36

Stack Implementation Using a LinkedList (Java) /** * Remove the most recently inserted item from the stack. * @throws UnderflowException if the stack is empty. */ public void pop( ) { if( isempty( ) ) throw new UnderflowException( "ListStack pop" ); topofstack = topofstack.next; } 37

Stack Implementation Using a LinkedList (Java) /** * Get the most recently inserted item in the stack. * Does not alter the stack. * @return the most recently inserted item in the stack. * @throws UnderflowException if the stack is empty. */ public AnyType peek( ) { if( isempty( ) ) throw new UnderflowException( "ListStack top" ); return topofstack.element; } 38

Example Applications for Stacks Postfix expression evaluation 2 + 3 * 4 vs 2 3 4 * + Checking for balanced delimiters - Balancing parenthesis/braces in a programming languages 39