Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 24 Lists, Stacks, and Queues
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Fundamentals of Python: From First Programs Through Data Structures
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
TCSS 342, Winter 2005 Lecture Notes
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
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.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Week 4 - Monday CS221.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Week 4 - Friday CS221.
Week 3 - Friday CS221.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues, Deques and Priority Queues
THURSDAY, OCTOBER 17 IN LAB
Queues, Deques and Priority Queues
Chapter 19: Stacks and Queues.
Queue.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Copyright © Aiman Hanna All rights reserved
Chapter 4 Queues.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues, Deques, and Priority Queues
Presentation transcript:

Week 3 - Friday

 What did we talk about last time?  Stacks  Array implementation of a stack

Bitmap Manipulator

Impromptu Student Lecture

 A queue is a simple data structure that has three basic operations (very similar to a stack)  EnqueuePut an item at the back of the queue  DequeueRemove an item from the front of the queue  FrontReturn the item at the front of the queue  A queue is considered FIFO (First In First Out) or LILO (Last In Last Out)

 Queues are useful whenever you want to keep track of the order of arrival  A line in a fast food restaurant  A job in a printer queue  A buffer for managing data

 A queue is a little bit harder to implement than a stack with an array  The trouble is that you're enqueuing and dequeuing from different ends  Removing something from the front seems to imply that you'll need to shift over all the contents of the array  Enter the circular array!

 A circular array is just a regular array  However, we keep a start index as well as a size that lets us start the array at an arbitrary point  Then, the contents of the array can go past the end of the array and wrap around  The modulus operator ( % ) is a great way to implement the wrap around

1. Starting array 2. Enqueue 9 3. Dequeue 4. Dequeue 5. Enqueue Dequeue Start Size = StartSize = StartSize = Start Size = Start Size = StartSize = 3

 Advantages:  Dequeue is Θ(1)  Front is Θ(1)  Disadvantages  Enqueue is Θ(n) in the very worst case, but not in the amortized case

public class ArrayQueue { private int[] data = new int[10]; private int start = 0; private int size = 0; public void enqueue(int value) {} public int dequeue() {} public int front() {} public int size() {} } public class ArrayQueue { private int[] data = new int[10]; private int start = 0; private int size = 0; public void enqueue(int value) {} public int dequeue() {} public int front() {} public int size() {} }

 Java does have a Stack class which extends Vector  The Deque (double ended queue) interface is preferred  A double ended queue can be used as either stack or queue Stack OperationDeque Method Push addFirst(T element) Pop removeFirst() Top peekFirst() Size size() Queue OperationDeque Method Enqueue addLast(T element) Dequeue removeFirst() Front peekFirst() Size size()

 Since Deque is an interface, we have to have classes that can implement it  ArrayDeque is an implementation of a double ended queue that uses a circular array for backing  Probably the best choice for both queues and stacks in terms of speed and memory use  addFirst() (push) is O(1) amortized  addLast() (enqueue) is O(1) amortized  removeFirst() (pop and dequeue) is O(1)  peekFirst() (top and front) is O(1)

 Good old LinkedList is an implementation of a double ended queue that uses a doubly linked list for backing  Generally slower than ArrayDeque, but the important operations are O(1) without being amortized  addFirst() (push) is O(1)  addLast() (enqueue) is O(1)  removeFirst() (pop and dequeue) is O(1)  peekFirst() (top and front) is O(1)

 A priority queue is like a regular queue except that items are not always added at the end  They are added to the place they need to be in order to keep the queue sorted in priority order  Not all requests are created equal  A higher priority job can come along and jump in front of a lower priority job  Unfortunately, we have to wait for the heap data structure to implement priority queues efficiently

 Linked lists

 Keep reading section 1.3  Finish Assignment 2  Due tonight by 11:59pm  Keep working on Project 1  Due next Friday, September 18 by 11:59pm