CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.

Slides:



Advertisements
Similar presentations
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Advertisements

0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Stacks and Queues. 2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you.
CS Data Structures II Review COSC 2006 April 14, 2017
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?
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Object Oriented Data Structures
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
The Java Collections Framework (JCF) Introduction and review 1.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
HW-6 Deadline Extended to April 27th
structures and their relationships." - Linus Torvalds
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Introduction to Computer Science for Majors II
Abstract Data Type Abstract Data Type as a design tool
Stacks.
Introduction to Data Structure
Midterm Review CSE116A,B.
Stacks, Queues, and Deques
Dynamic Array: Implementation of Stack
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
Presentation transcript:

CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson

HHW1 still out! DDue Friday, 1/30 at 11:59pm

 “Remember these, you shall, hmm?”

 Java basics  OO and inheritance  Arrays and ArrayList  Abstract data types  Stacks  Queues

 Data type  A data type is a set of values and a set of operations on those values ▪ Specific implementations, e.g. 32-bit signed int.  Abstract data type (ADT)  An abstract data type is a data type whose internal representation is hidden from the client ▪ No implementation, more a mathematical description

 ADTs are implemented in Java as classes  Instance variables are private (hidden from the client)  Instance methods may be public (specified in the API) or private (organize the computation and hidden from the client)  Application programming interface (API)  specifies the behavior of an ADT including constructors and instance methods  Using ADTs  Create objects of implemented ADTs  Invoke instance methods to operate on data-type values

 Standard system ADTs  Integer, Double, String, StringBuilder …  Data oriented ADTs – facilitate organizing and processing data  Point2D, Interval1D, Date, …  Collection ADTs – facilitate organizing and manipulating collections of data  Stack, Queue, Priority Queue, ArrayList, …

 Definition and API  Intended behavior  Implementation  How to implement  Applications  How to use

Stacks A stack stores a set of elements but with only two main operations: Push: add an element to the top of the stack Pop: remove the top element of the stack. Pop always removes the last element that’s added to the stack. This is called LIFO (Last- In-First-Out).

Stacks –Examples A can of tennis balls – Imagine the entire can represents an array, and each ball is an element. – It only allows access to one element at a time: the last element. – If you remove the last element, you can then access the next-to-last element. – There is no way to directly access the element at the bottom.

Stacks – Another Example A dynamic list of tasks you perform everyday: – Imagine you start your day by working on task A. – At any time you may be interrupted by a co- worker asking you for temporary help on task B. – While you work on B, someone may interrupt you again for help on task C. – When you are done with C, you will resume working on B. – Then you go back to work on A. – Think about the sequence of tasks you perform.

Stack Example

 Stacks are typically used as a programmer’s tool, not for storage  used for handling program calls and returns  used for data manipulations and problem solving ▪ E.g. reversing  Arrays (and others) are typically used as data storage structures ▪ E.g. personal records, inventories …

 Definition and API  Intended behavior  Implementation  How to implement  Applications  How to use

 Underflow: what happens if pop from an empty stack?  Throw exception  Overflow: what happens if push to a full stack?  Exception again  Use resizing array

 What’s the cost of pushing/adding to a stack of size N?  Case 1: array resizing not required  Case 2: array resizing required

 Definition and API  Intended behavior  Implementation  Implementation using fixed size array ▪ FixedCapacityStackOfStrings.java  Implementation using resizable array ▪ ResizingArrayStack.java (using generics)  Implementation using generics  Applications  How to use

 Stack of strings using fixed-capacity array: FixedCapacityStackOfStrings.java FixedCapacityStackOfStrings.java  Generic stack using fixed-capacity array: FixedCapacityStack.java FixedCapacityStack.java  Generic stack using a resizing array: ResizingArrayStack.java ResizingArrayStack.java  Generic stack using a linked list (later): Stack.java Stack.java