CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

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.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Chapter 12: Data Structures
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Building Java Programs
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
The Java Collections Framework (JCF) Introduction and review 1.
Information and Computer Sciences University of Hawaii, Manoa
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data structures Abstract data types Java classes for Data structures and ADTs.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
CS-2852 Data Structures Week 5, Class 2 – Testing and Stacks Announcement: Lab Demos - 2/3 & 4 on Friday Testing  Definitions, Example  (tentative) Testing.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Computer Engineering Rabie A. Ramadan Lecture 6.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Winter 2005CS-2851 Dr. Mark L. Hornick 1 Recursion.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
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.
Marcus Biel, Software Craftsman
QueueStack CS1020.
Chapter 12: Data Structures
Data Structures and Database Applications Queues in C#
Building Java Programs
Road Map CS Concepts Data Structures Java Language Java Collections
structures and their relationships." - Linus Torvalds
THURSDAY, OCTOBER 17 IN LAB
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
JCF Collection classes and interfaces
Road Map CS Concepts Data Structures Java Language Java Collections
Abstract Data Type (ADT)
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
structures and their relationships." - Linus Torvalds
Presentation transcript:

CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure

CS-2851 Dr. Mark L. Hornick 2 The Stack data structure The semantics (behavior) of a Stack is defined as follows: 1. Elements can only be inserted and removed from the front of a Stack This is known as Last-In, First-Out (LIFO)  Less commonly: First-Out, Last-In (FOLI) 2. Random-access is not defined – you cannot access the “middle” of the Stack No get(index) behavior No add(index, element) behavior

CS-2851 Dr. Mark L. Hornick 3 Stack – behavioral methods Principal methods that define behavior: 1. push() – place an element on (top of) the stack 2. pop() – return and remove an element from the (top of the) stack 3. peek() – return the top element of the stack Without removing (popping) it The naming for the structure and the methods is an analogy for how the data elements within the structure are accessed

CS-2851 Dr. Mark L. Hornick 4 What is the best way to implement a Stack? 1. Derive from ArrayList? Advantages? Disadvantages? 2. Derive from LinkedList? Advantages? Disadvantages? Does the internal implementation really matter as long as the resulting collection behaves like a Stack?

CS-2851 Dr. Mark L. Hornick 5 JCF implementation of Stack extends Vector …with the 3 methods necessary for Stack behavior (push, pop, peek) The extended Stack interface conforms to the idea of the stack abstraction, but… it is derived from Vector (like an ArrayList, but less efficient!) All Vector methods are inherited by Stack Thus there are methods to access the interior of the stack which violates the concept of a stack! So a JCF Stack is not a “pure” stack

CS-2851 Dr. Mark L. Hornick 6 Queue data structure Once again: an ordered collection of elements Classical semantics (behavior): 1. Elements can only be inserted at the back and removed from the front of the collection Alternately: inserted at the front and removed from the back 2. This is known as First-In, First-Out (FIFO)

CS-2851 Dr. Mark L. Hornick 7 Queue – behavioral methods offer() – place an element at the back of the queue poll() or remove() – return and remove an element from the front of the queue poll() returns null if the queue is empty remove() throws an exception if the queue is empty peek() or element() – return (without removing) the element at the front of the queue peek() returns null if the queue is empty element() throws an exception if the queue is empty

CS-2851 Dr. Mark L. Hornick 8 JCF Queue is an Interface Unlike Stack, there is no implementation of Queue by itself. The interface just declares the methods that a class implementing Queue would have Queue extends the Collection interface: public interface Queue extends Collection { boolean offer(E o); // add to queue E element(); // get w/o remove E peek(); // get w/o remove E poll(); // get w/ remove E remove(); // get w/ remove What does this imply? What behavior does a Collection have?

CS-2851 Dr. Mark L. Hornick 9 JCF definition of Queue interface is weird The Queue interface conforms to the idea of the queue abstraction, but it is extended from Collection, and so All Collection methods must be implemented by anything that implements Queue There will be methods to access the interior of the queue which violates the queue abstraction ! So you wouldn’t have a true queue

CS-2851 Dr. Mark L. Hornick 10 What is the best way to implement a Queue? 1. Derive from ArrayList? Advantages? Disadvantages? 2. Derive from LinkedList? Advantages? Disadvantages?

CS-2851 Dr. Mark L. Hornick 11 More weirdness: LinkedList implements the Queue interface! So LinkedList is a Collection which is also a List which is also a Queue (and is also a Deque…) What does this imply? LinkedList behaves like them all!