1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.

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 Linked Lists
Chapter 9: Data Structures I
Stack & Queues COP 3502.
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  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CHAPTER 7 Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
COMP 110 Introduction to Programming Mr. Joshua Stough.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
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.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Objectives of these slides:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
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,
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
0 of 36 Andries van Dam  /05/15 Reminder: TA Hour Policies If you hit a bug, you must be able to show significant effort of fixing it on your own.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Review Array Array Elements Accessing array elements
Chapter 12: Data Structures
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 2: Stacks and Queues
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
CMPT 225 Lecture 8 – Queue.
Data Structures & Programming
Presentation transcript:

1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException

2 Purpose: In this lecture series we will discuss two more ADTs The Stack and Queue data types are used to process data in a linear fashion

3 Resources: Barrons Chapter 9 p.300 (EXCEPT Priority Queuesp ) Java Essentials Chapter 19 p.757 Java Essentials Study Guide Chapter 16 p.281 Java Methods Chapter 3 p.61

4 Handouts: 1. Stack and Queue Interfaces & Implementations 2.The Following Java Code: Stack.java Queue.java ArrayStack.java ListQueue.java

5 Intro: A STACK is a linearly ordered list A STACK is a data structure used for storing and retrieving data elements in such a way that the element stored last will be retrieved first. LIFO --- last in first out

6 Intro: A QUEUE is a data structure used for temporary storage from which the data elements are retrieved in the same order as they were stored. FIFO – first in first out Used for processing events that have to be processed in the order of their arrival (events are buffered in the queue)

7 Stacks: Think of the mountain of clothes piled in a corner of your room As you hurry to dress so as not to be late for school, you begin your search from the TOP of the clothes pile The last stuff to be thrown on to the pile is the first to be examined This is a “messy” stack

8 TO implement a Stack ADT, you must provide a way to store (push) and remove (pop) elements from a Stack Elements on the stack should be of the same type (or in the same class hierarchy) A Stack has no limits as to its size

9 TO add an element on to the stack you “Push” that item and it goes to the front of the stack TO remove an element from the stack, you can only “Pop” off the element currently at the top of the stack PUSH --- adds elements to the top of the stack POP --- removes the element from the top of the stack

10 These operations should be in Constant time O(1) Other behaviors of the Stack should return the top element of the Stack without removing it (peekTop) and to see if the Stack has any elements left in it (isEmpty)

11 For testing purposes, we will use the following as the standard Stack Interface (similar to ListNode when dealing with Linked Lists)

12 public interface Stack { // postcondition: returns true if stack is empty, false otherwise boolean isEmpty(); // precondition: stack is [e1, e2,..., en] with n >= 0 // postcondition: stack is [e1, e2,..., en, x] void push(Object x); // precondition: stack is [e1, e2,..., en] with n >= 1 // postcondition: stack is [e1, e2,..., e(n-1)]; returns en // throws an unchecked exception if the stack is empty Object pop(); // precondition: stack is [e1, e2,..., en] with n >= 1 // postcondition: returns en // throws an unchecked exception if the stack is empty Object peekTop(); }

13 RUNTIME STACK Remember the “runtime stack” ? It uses stacks to handle function calls The stack holds the function specific data PLUS the memory address of the NEXT instruction to execute Elements are “on the stack”

14 The stack is controlled by the two operations:PUSH & POP Possible Implementation of a Stack is a singly linked list enhanced by an additional reference to the tail of the list Elements are added at the tail of the list and removed at the head of the list. However, lets look at an ArrayList Implementation

15 public class ArrayStack implements Stack { private ArrayList array; public ArrayStack() { array = new ArrayList(); } public void push(Object x) { array.add(x); } public Object pop() { return array.remove(array.size() - 1); } public Object peekTop() { return array.get(array.size() - 1); } public boolean isEmpty() { return array.size() == 0; }

16 To use the Stack: // SPVM MyStack mS = new ArrayStack(); mS.push(new String (“Billy”); mS.push(new String (“Joe”); mS.push(new String (“Bob”); mS.push(new String (“Sally”); mS.push(new String (“Lucy”); The Stack Should now look like this: Lucy Sally Bob Joe Billy

17 Object o = mS.pop( ); String s = (String)mS.pop( ); String y = (String)mS.peekTop( ); Boolean empty = mS.isEmpty( ); The Stack Should now look like this: Bob Joe Billy The String y contains “Bob” The Boolean empty is False

18 Errors: The pop and peekTop methods can throw an Unchecked Exception IndexOutOfBOundsException or NoSuchElementException in cases where the stack is empty

19 If you use the ArrayList or the LinkedList Java class to implement your Stack, then these errors are handled, for you, from within these classes

20 Queues: Its lunch time and you leave class early to get into the food line “Dr.Nick” dashes as fast as he can and is first to grab a tray and get into line After Dr.Nick, “Naum” trips over a floor tile and skins his knee

21 As a result, “Alden” stops to help wipe away Naum’s tears and to give him a Scooby Doo bandaid So, Alden winds up behind Dr.Nick and Naum follows Since the line is implemented as a Queue, Dr.Nick gets the best lunch choices and Naum gets the crumbs

22 TO implement a Queue ADT, you must provide a way to store (enqueue) and remove (dequeue) elements from a Queue Elements on the Queue should be of the same type (or in the same class hierarchy) A Queue has no limits as to its size

23 TO add an element on to the Queue you “enqueue” that item and it goes to the end of the Queue TO remove an element from the Queue, you can only “dequeue” off the element currently at the top of the Queue

24 ENQUEUE --- adds elements to the end of the Queue DEQUEUE --- removes the element from the top of the Queue These operations should be in Constant time O(1)

25 Other behaviors of the Queue should return the top element of the Queue without removing it (peekFront) and to see if the Queue has any elements left in it (isEmpty) For testing purposes, we will use the following as the standard Queue Interface:

26 public interface Queue { // postcondition: returns true if queue is empty, false otherwise boolean isEmpty(); // precondition: queue is [e1, e2,..., en] with n >= 0 // postcondition: queue is [e1, e2,..., en, x] void enqueue(Object x); // precondition: queue is [e1, e2,..., en] with n >= 1 // postcondition: queue is [e2,..., en]; returns e1 // throws an unchecked exception if the queue is empty Object dequeue(); // precondition: queue is [e1, e2,..., en] with n >= 1 // postcondition: returns e1 // throws an unchecked exception if the queue is empty Object peekFront(); }

27 Ring Buffer An array used in a circular manner. Adjust the pointer that defines the “logical” first array element. The state of the queue is maintained with the help of 2 indices, FRONT and REAR.

28 Front points to the first element in the queue (returned by the next call to the dequeue) Rear points to the empty slot following the last stored element. Enqueue method stores the next element in the slot pointed to by the rear and increments the rear index.

29 PC’s have a keyboard queue implemented as a ring buffer. When a key is pressed its code does not go directly to the active program but is placed in the keyboard buffer until the program requests it.

30 Lets look at a LinkedList Implementation:

31 public class ListQueue implements Queue { private LinkedList list; public ListQueue() { list = new LinkedList(); } public void enqueue(Object x) { list.addLast(x); } public Object dequeue() { return list.removeFirst(); } public Object peekFront() { return list.getFirst(); } public boolean isEmpty() { return list.size() == 0; }

32 To use the Queue: // SPVM MyQueue mQ = new ListQueue(); mQ.enqueue(new String (“Billy”); mQ.enqueue(new String (“Joe”); mQ.enqueue(new String (“Bob”); mQ.enqueue(new String (“Sally”); mQ.enqueue(new String (“Lucy”); The Stack Should now look like this: Billy Joe Bob Sally Lucy

33 Object o = mQ.dequeue( ); String s = (String)mQ.dequeue( ); String y = (String)mQ.peekFront( ); Boolean empty = mQ.isEmpty( ); The Stack Should now look like this: Bob Sally Lucy The String y contains “Bob” The Boolean empty is False

34 Queues are best when simulating bank lines, or any other system where there is a “First Come First Served” basis

35 Lets look at Barrons Redial Telephone Feature on Page 305

36 TPS:Implement a Queue using an Array (or ArrayList) Implement the Possible Unchecked Exceptions Draw array implementation on board (RingBuffer)

37 AP AB Subset Requirements: Although there is no standard Java Interface we need to be able to work with the AP interfaces for Stacks and Queues (handouts) Students are also responsible for understanding that these ADT’s can be implemented as an array or a linked list, although you will NOT be tested on any SPECIFIC implementation

38 AP AB Subset Requirements: For our reference purposes we will look at an Array implementation of a stack called ArrayStack.java and we will look at a LinkedList implementation of a Queue called ListQueue.java

39 AP AB Subset Requirements: You will see Multiple Choice questions regarding analysis of Stack and Queue implementations and processes Multiple Choice questions may focus on your ability to evaluate the effectiveness of and to determine the best use of Stacks and Queues

40 Projects: Barrons M/C Questions Chapter 9 p.310 Do ALL Questions Except the following: # 14, 15 Decimal to Binary Expression McDonalds Palendrome

41 TEST FOLLOWS COMPLETION OF PROJECTS !!!