Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 12: Data Structures
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
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.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
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.
Microsoft® Small Basic
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Microsoft® Small Basic Stacks and Arrays Estimated time to complete this lesson: 1 hour.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Data Structures and Abstractions with Java, 4e Frank Carrano
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
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’
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,
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Understanding Data Types and Collections Lesson 2.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue.
Arrays and Collections Tonga Institute of Higher Education.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
Stacks1 of 32 STACKS What are Stacks? Creating a Stack Pushing and Popping Testing for an Empty Stack Burgers! © Pearson Education.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Click to edit Master text styles Stacks Data Structure.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Chapter 20 Generic Classes and Methods
Pointers and Linked Lists
Tonga Institute of Higher Education
Abstract Data Type (ADT)
Corresponds with Chapter 5
Presentation transcript:

Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview ● Stacks and Queues – Data structure holding a collection of objects, ordered by when they were inserted into the structure. – Stack can access newest object. – Queue can access oldest object.

Overview ● 4.1 – The Stack Interface ● Stack behavior ● Generic classes ● 4.2 – The Java Call Stack ● 4.4 – The Queue Interface

The Stack Interface ● Stack – Data structure holding several items. – New items added to the top of the stack. ● Pushing – Push items onto the stack ● Pop – Pop items off of the stack or extract the topmost item. ● Peek – Peek at the top item or check if the stack is empty.

The Stack Interface

● Iast-in, first-out or LIFO policy – Last item pushed on the stack is next item popped off the stack. – Example: ● A stack of plates.

The Stack Interface

● Generics – The Stack interface describes a stack of Objects. ● s.push(new Die(3)); – When we pop a Stack we get back an Object. – We usually cast it to a more specific class so we can use specific functionality. ● ((Die)(s.pop())).roll(); – This is not only inconvenient, it is slightly unsafe. – We must remember what we put on the Stack, or we might try to cast something that is not really a Die to be a Die.

The Stack Interface ● Generic classes and interfaces – Has one or more type parameters. – We have to specify a type for each type of parameter. ● Stack s = new Stack (): ● s.push(new Die()); s.pop().roll(); – Java won't let us push anything of the wrong type onto this Stack. – The pop() method's return type is specified by the type parameter.

The Stack Interface

● In UML diagrams type parameters are shown as dashed boxes are at the upper right of a class.

The Stack Interface

● Generics do not play well with arrays – We can declare an array field or variable involving type parameters, like the field stacks. – We cannot actually allocate an array involving a type parameter. ● Example: stacks = new Stack [4] – Assigning each cell in the array a generic type parameter produces a warning, but not an error.

The Stack Interface

● The pop() method returns a value, but we don't use that value in this particular program.

The Stack Interface

The Call Stack

● The hypotenuse() method invokes the square() method twice. ● How does Java know where to go when it finishes an invocation of square()? ● Java keeps track of what it was doing before the invocation started by using a stack. – This stack is called a call stack.

The Call Stack ● Every time a method is invoked, a call frame is created. – The call frame keeps track of the current state of the method. – It stores any arguments or variables for the method. – It also keeps track of how far along the method has proceeded.

The Call Stack

● Knowledge of the call stack helps us understand some of Java's error messages. ● The error message shows a stack trace (a snapshot of the call stack) indicating what was going on when the program crashed.

The Call Stack

The Queue Interface ● Queue – Very similar to a stack. – Items are inserted in one end (the back) and removed from the other end (the front). – first-in, first-out, or FIFO

The Queue Interface

Summary ● Stacks and queues are collections of objects. – Stack follows a last-in, first-out policy. ● Objects are pushed onto and popped off the top. – Queue follows a first-in, first-out policy. ● Objects are added to the back and removed from the front.

Summary ● Java 1.5 introduces generic types and interfaces. – Generics greatly reduces the amount of casting we have to do when using general-purpose data structures.

Summary ● Call Stack – Whenever a method is invoked, a new call frame is pushed onto the stack. ● This keeps track of any variables in the method, as well as how far the method has progressed.