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

Similar documents
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.

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

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

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Stacks Goodrich, Tamassia

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.

Stacks Goodrich, Tamassia Stacks

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

COMP9024: Data Structures and Algorithms

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

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

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures

csci 210: Data Structures Stacks and Queues

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

DataStruct 5. Stacks, Queues, and Deques

Data Structures and Algorithms " Arrays and Linked Lists!

Stacks Goodrich, Tamassia, Goldwasser Stacks

data structures and algorithms lecture 6

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

Lecture 3 Linear Data Structures

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

This lecture. Abstract Data Types (ADTs) The Stack ADT ( 4.2) Stacks. Stack Interface in Java. Exceptions. Abstract data types Stacks Queues

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

Data Structures Lecture 5

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

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

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

Stacks Fall 2018 Margaret Reid-Miller

How many times is the loop executed? middle = (left+right)/2; if (value == arr[middle]) return true;

CS212:Data Structure

CSC 273 Data Structures

Data Structures And Algorithms

CS350: Data Structures Stacks

Interfaces & Generics

1. Stack Implementation Using 1D Array

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Apply to be a Meiklejohn! tinyurl.com/meikapply

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

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

Standard ADTs. Lecture 19 CS2110 Summer 2009

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Foundations of Data Structures

CT 229 Object-Oriented Programming Continued

CSE 143 SAMPLE MIDTERM

Data Structures G5029

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Associate Professor Dr. Raed Ibraheem Hamed

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

CIS Fall Data Structures Midterm exam 10/16/2012

LIFO : Last In First Out

Basic Data Structures

1 P age DS & OOPS / UNIT II

CSC 222: Computer Programming II. Spring 2005

Chapter 2. Stack & Queues. M hiwa ahmad aziz

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Linear Data Structures

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Basic Data Structures 1 / 24

Dynamic Data Structures

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

infix expressions (review)

Queues Fall 2018 Margaret Reid-Miller

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

CE204 Data Structures and Algorithms Part 2

Top of the Stack. Stack ADT

Application of Stack (Backtracking)

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

File Input and Output

8. Fundamental Data Structures

CSCD 326 Data Structures I Stacks

Abstract Data Types. Abstract Data Types

More Data Structures (Part 1) Stacks

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

DNHI Homework 3 Solutions List, Stacs and Queues

16. Dynamic Data Structures

Linked List Implementation of Queues

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

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

ADTs, Arrays, Linked Lists

What can go wrong in a Java program while running?

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

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

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

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

No Aids Allowed. Do not turn this page until you have received the signal to start.

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

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

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

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

PA3 Design Specification

Stacks and Queues. David Greenstein Monta Vista

Transcription:

Stacks (5.1) CSE 2011 Winter 2011 26 January 2011 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Example: ADT modeling a simple stock trading system The data stored are buy/sell orders The operations supported are order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order) Error conditions: Buy/sell a nonexistent stock Cancel a nonexistent order Stacks 2 1

Stacks: LIFO Insertions and deletions follow the Last-In First-Out rule Example applications: Undo operation in a text editor History of visited web pages Sequence of method calls in Java 3 Method Stack in the JVM The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 Stacks 4 2

Stack ADT Data stored: arbitrary objects Operations: push(object): inserts an element object pop(): removes and returns the last inserted element Other useful operations: object top(): returns the last inserted element without removing it 5 Error Conditions push(object) object pop() object top() Exceptions are thrown when an operation cannot be executed. Execution of pop() or top() on an empty stack throws EmptyStackException. Another useful operation: booleanisempty(): returns true if the stack is empty; false otherwise. 6 3

Stack Operations push(object) object pop() object top() boolean isempty() Still another useful operation: intsize(): returns the number of elements in the stack Any others? Depending on implementation 7 Stack Interface in Java Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Different from the built-in Java class java.util.stack public interface Stack { public int size(); public boolean isempty(); public Object top() throws EmptyStackException; public void push(object o); public Object pop() throws EmptyStackException; Stacks 8 4

Array-based Implementation An array S of maximum size N A variable t that keeps track of the top element in array S How to initialize t? Top element: S[t] push( ), pop( ): how to update t? Stack is empty, isempty( ):? Number of elements in the stack, size():? 9 Pseudo-code Algorithm size(): return (t + 1); Algorithm isempty(): return (t < 0); Algorithm top(): if (isempty()) throw StackEmptyException; return S[t]; Algorithm pop(): if (isempty()) throw StackEmptyException; temp = S[t]; t = t 1; return temp; Optimization: set S[t] to null before decrementing t. Homework: implement pop() without any temp variable. 10 5

Method push() Algorithm push(object): t = t + 1; S[t] = object; The array may become full push() method will then throw a FullStackException Limitation of array-based implementation One solution: extend the stack Algorithm push(object): if (size() == N) throw FullStackException; t = t + 1; S[t] = object; 11 Array-based Stack in Java public class ArrayStack implements Stack { // holds the stack elements private Object S[ ]; // index to top element private int top = -1; // constructor public ArrayStack(int capacity) { S = new Object[capacity]); public Object pop() throws EmptyStackException { if isempty() throw new EmptyStackException ( Empty stack: cannot pop. ); Object temp = S[top]; // facilitates garbage collection S[top] = null; top = top 1; return temp; Stacks 12 6

Performance of Array Implementation Each operation runs in O(1) time (no loops, no recursion) Array-based implementation is simple, efficient, but The maximum size N of the stack is fixed How to determine N? Not easy! Alternatives? Extendable arrays Linked lists (singly or doubly linked???) 13 Linked List Implementation Singly or doubly linked list? Where should the top be, head or tail? A B C D 14 7

push() Method 15 pop() Method 16 8

Analysis of Linked List Implementation Space usage: O(n) n = number of elements in the stack Each operation runs in O(1) time No limit on the stack size, subject to available memory (run-time erroroutofmemoryerror) Java code: textbook, p. 212 17 Homework List-based and array-based operations all run in O(1) time. List-based implementation imposes no limit on the stack size, while array-based implementation does. Is list-based implementation better? Can we perform push() and pop() at the tail of the linked list? Analyze the running time in this case. Study the linked list implementation of stacks in Java on p. 212. 18 9

More Applications of Stacks Reversing an array using a stack (5.1.4) Matching parentheses, brackets, and quotes in Java files (5.1.5) 19 Next time... Queues (5.2) 20 10