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

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

What can we do with Coin dispenser?

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

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

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

Stack Abstract Data Type

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

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.

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Lists are great, but. Stacks 2

infix expressions (review)

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Linear Data Structure

1. Stack Implementation Using 1D Array

CMPT 225. Stacks-part2

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

Lecture 12 ADTs and Stacks

Stack and Its Implementation

An Introduction to Trees

CMPT 225. Lecture 9 Stack

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structures and Algorithms

CS 211. Project 5 Infix Expression Evaluation

March 13/2003 Jayakanth Srinivasan,

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

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Stacks, Queues and Hierarchical Collections

Stacks. 1 Introduction. 2 The Stack ADT. Prof. Stewart Weiss. CSci 235 Software Design and Analysis II Stacks

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

CSCD 326 Data Structures I Stacks

Lecture 4 Stack and Queue

Array Based Lists. Collections

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

CS 206 Introduction to Computer Science II

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Lecture No.04. Data Structures

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

CS 206 Introduction to Computer Science II

Basic Data Structures

CS W3134: Data Structures in Java

Stacks and Queues. David Greenstein Monta Vista

Assignment 5. Introduction

CS 211 Programming Practicum Spring 2017

CSC 273 Data Structures

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CSC 222: Computer Programming II. Spring 2005

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

Data Structures G5029

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks and Queues. Chapter Stacks

Application of Stack (Backtracking)

Stacks. Common data structures - useful for organizing data for specific tasks Lists Stacks - an Abstract Data Type

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

Lecture Data Structure Stack

The Stack and Queue Types

Stacks. Revised based on textbook author s notes.


Stacks and their Applications

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

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

More Data Structures (Part 1) Stacks

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

CMPS 390 Data Structures

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Basic Data Structures 1 / 24

CS350: Data Structures Stacks

ADTs Stack and Queue. Outline

Data Structures Week #3. Stacks

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

15. Stacks and Queues

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CSE 214 Computer Science II Stack

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Project 1: Implementation of the Stack ADT and Its Application

Data Abstraction and Specification of ADTs

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

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

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

CS 211 Programming Practicum Spring 2018

Stacks, Queues (cont d)

Chapter 2. Stack & Queues. M hiwa ahmad aziz

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

! Instan:a:ng a Group of Product objects. ! InstanEaEng a Group of Friend objects. ! CollecEons can be separated into two categories

CS2113 Lab: Collections 10/29/2018

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

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Assignment 4 Publication date: 27/12/2015 Submission date: 17/01/ :59 People in-charge: R. Mairon and Y. Twitto

Programming Abstractions

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

C Sc 127B Practice Test 2 Section Leader Your Name 100pts

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Transcription:

Linear, time-ordered structures CS00: Stacks n Prichard Ch 7 n Data structures that reflect a temporal relationship order of removal based on order of insertion n We will consider: first come,first serve n first in first out - FIFO (queue) take from the top of the pile n last in first out - LIFO (stack) What can we do with coin dispenser? n push a coin into the dispenser n pop a coin from the dispenser n see the coin on top n check whether this dispenser is empty or not

Stacks Stack Operations n Last In First Out (LIFO) structure A stack of pancakes or dishes n Add/Remove done from same end top n isempty(): determine whether stack is empty n push(): add a new item to the stack n pop(): remove the item added most recently n peek(): retrieve the item added most recently n createstack(): create an empty stack n removeall(): remove all the items 6 Applications - the run-time stack n Nested method calls tracked on call stack (aka run-time stack) First method that returns is the last one invoked n Element of call stack - activation record parameters local variables return address: pointer to next instruction to be executed in calling method Checking for balanced braces n How can we use a stack to determine whether the braces in a string are balanced? abc{defg{ijk}{l{mn}}op}qr abc{def}}{ghij{kl}m http://enwikipediaorg/wiki/image:call_stack_layoutsvg 7 8

Pseudocode String abc{{def}ijm} while ( not at the end of the string){! if (the next character is a { ){! astackpush( { )! else if (the character is a } ) {! openbrace = astackpop()! }! a b c { { d e f } while ( not at the end of the string){! i j m if (the next character is a { ){! } astackpush( { )! else if (the character is a } ) {! openbrace = astackpop()! }! Peudocode with example { { 9 Expressions Parsing a Postfix Expression Types of Algebraic Expressions while there are input tokens left read the next token if the token is a value push it onto the stack else //the token is a function taking n arguments pop the top n values from the stack and evaluate the function push the result on the stack If there is only one value in the stack return it as the result else throw an exception" Prefix Postfix (RPN) Infix Prefix and postfix are easier to parse No ambiguity Postfix: operator applies to the operands that immediately precede it Examples: **- **- 0 operands are written in the conventional way

Stack Methods Reference-Based Implementation push(in newitem:stackitemtype) throws StackException adds a new item to the top of the stack Exception when insertion fails pop():stackitemtype throws StackException deletes the item at the top of the stack and returns it Exception when deletion fails peek():stackitemtype {query} throws StackException returns the top item from the stack, but does not remove it Exception when retrieval fails Preconditions? Postconditions? TOP A Node 0 Your Object To be Stacked 80 A Reference to the next Node 60 Implementation that uses List private Node top;! public void push (Object newitem){!! top = new Node(NewItem,top)!! public Object pop() throws StackException{! if (!isempty()){! Node temp = top;! top = topnext;! return tempitem;! } else { exception handling List position 0 Listsize() 0 80 60 Top of the stack 6

List based Implementation Comparison of Implementations public void push(object newitem){! listadd(0, newitem);! public Object pop() throws StackException{! if (!listisempty()){! Object temp = listget(0);! listremove(0);! return temp;! } else exception handling! n Options for Implementation: Array based implementation ArrayList based implementation Reference based implementation n What are the advantages and disadvantages of each implementation? public void popall(){!!listremoveall();! 7 8 Stack API in Java public class Stack<E>extends Vector<E>! Implemented Interfaces: Iterable<E>, Collection<E>, List<E>, RandomAccess! n Stack extends Vector with operations that allow a vector to be treated as a stack (push, pop, peek, empty, search) Stacks and Recursion n Most implementations of recursion maintain a stack of activation records n Backtracking example n Within recursive calls, the most recently executed call is stored at the top of the stack n Store and access the same point of the data structure 9 0