Week 4 - Monday CS221.

Slides:



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

Chapter 24 Lists, Stacks, and Queues
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a 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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
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.
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.
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 2 - Friday.  What did we talk about last time?  Computing Big Oh.
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.
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,
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Week 15 – Monday.  What did we talk about last time?  Tries.
© 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.
Elementary Data Structures
Review Array Array Elements Accessing array elements
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
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.
Data Structure By Amee Trivedi.
G64ADS Advanced Data Structures
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.
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Week 4 - Friday CS221.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Program based on queue & their operations for an application
Csc 2720 Data structure Instructor: Zhuojun Duan
John Hurley Cal State LA
Sequences and Iterators
Week 3 - Friday CS221.
CSE 116/504 – Intro. To Computer Science for Majors II
Queues Queues Queues.
Week 15 – Monday CS221.
LINKED LISTS CSCD Linked Lists.
Topic 16 Queues Adapted from Mike Scott’s materials.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
THURSDAY, OCTOBER 17 IN LAB
Stacks, Queues, and Deques
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
CSC 143 Queues [Chapter 7].
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Chapter 4 Queues.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Week 4 - Monday CS221

Last time What did we talk about last time? Stacks Implementing stacks with arrays

Questions?

Project 1 Bitmap Manipulator

Queues

Queue A queue is a simple data structure that has three basic operations (very similar to a stack) Enqueue Put an item at the back of the queue Dequeue Remove an item from the front of the queue Front Return the item at the front of the queue A queue is considered FIFO (First In First Out) or LILO (Last In Last Out)

Application of queues 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

Circular array implementation 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!

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

Circular array example Starting array Enqueue 9 Dequeue Dequeue Enqueue 14 7 18 3 21 Start Size = 4 3 21 9 Start Size = 3 7 18 3 21 9 Start Size = 5 14 3 21 9 Start Size = 4 18 3 21 9 Start Size = 4 14 21 9 Start Size = 3

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

Circular array implementation 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() {} }

Circular Array Front

Circular Array Get Size

Circular Array Enqueue

Circular Array Dequeue

JCF Stacks and Queues

Deque<T> 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 Operation Deque Method Push addFirst(T element) Pop removeFirst() Top peekFirst() Size size() Queue Operation Deque Method Enqueue addLast(T element) Dequeue removeFirst() Front peekFirst() Size size()

ArrayDeque<T> 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)

LinkedList<T> 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)

Priority queues 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

Purpose and design of linked lists Impromptu student lecture

X Linked lists What is a linked list? Why not just use (dynamic) arrays for everything? head 23 47 58 X

Pros Insert at front (or back) Delete at front (or back) Arbitrary amounts of storage with low overhead

Cons Search O(n) Go to index Potentially significant memory overhead if data is small Much easier to make pointer and memory errors (especially in C/C++)

Implementations

Levels of flexibility Class protecting nodes implementation Generic class providing nodes with arbitrary type Generic class with the addition of iterators

Wait, what's an iterator? I'm glad you asked They allow a collection to be used in a foreach loop So, what's a foreach loop? It allows you to read (but not change) each value in a list public static int sum( int[] array ) { int total = 0; for( int value: array ) total += value; return total; }

So what? Foreach loops work for any iterable list of any type public static double weigh(Wombat[] list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(ArrayList<Wombat> list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(LinkedList<Wombat> list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; }

X Singly linked list Node consists of data and a single next pointer Advantages: fast and easy to implement Disadvantages: forward movement only head 23 47 58 X

Doubly linked list Node consists of data, a next pointer, and a previous pointer Advantages: bi-directional movement Disadvantages: slower, 4 pointers must change for every insert/delete X head 23 47 58 X tail

Interview question You are given a singly linked list It may have a loop in it, that is, a node that points back to an earlier node in the list If you try to visit every node in the list, you’ll be in an infinite loop How can you see if there is a loop in a linked list?

Upcoming

Next time… Implementation of a linked list Circular linked lists and skip lists Implementing a stack with a linked list Keep reading section 1.3

Reminders Keep reading section 1.3 Keep working on Project 1 Due this Friday, September 22 by 11:59pm