COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)
|
|
- Oswald Andrews
- 5 years ago
- Views:
Transcription
1 COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)
2 Recall: The Queue ADT A data structure in which elements enter at one end and are removed from the opposite end is called a queue. 2 main operations: enqueue dequeue
3 Recall: Formal Specification Our queues are generic the type of object held by a particular queue is indicated by the client. We provide observer operations isempty and isfull. We create a QueueInterface that defines the signatures of the queue methods that do not depend on the boundedness of the queue. We create a BoundedQueueInterface and an UnboundedQueueInterface, which extend QueueInterface.
4 Array-Based Implementation for unbounded queue Class ArrayUnbQueue that implements the UnboundedQueueInterface. The trick here is to create a new larger array, when needed, and copy the structure into the new array. No need for isfull method.
5 Array-Based Implementation for unbounded queue Class ArrayUnbQueue that implements the UnboundedQueueInterface. The trick here is to create a new larger array, when needed, and copy the structure into the new array. No need for isfull method.
6 Array-Based Implementation for unbounded queue Class ArrayUnbQueue that implements the UnboundedQueueInterface. The trick here is to create a new larger array, when needed, and copy the structure into the new array. No need for isfull method.
7 Unbounded vs. bounded implementation: what changes? enqueue method to increase the capacity of the array if it has run out of space. we implement it as a separate method named enlarge. Then, the enqueue method can start with: if (numelements == queue.length) enlarge();
8 Unbounded vs. bounded implementation: what changes? enqueue method to increase the capacity of the array if it has run out of space. we implement it as a separate method named enlarge. Then, the enqueue method can start with: if (numelements == queue.length) enlarge();
9 Unbounded vs. bounded implementation: what changes? enqueue method to increase the capacity of the array if it has run out of space. we implement it as a separate method named enlarge. Then, the enqueue method can start with: if (numelements == queue.length) enlarge();
10 Options for the enlarge method We could set a constant increment value or multiplying factor within the class. We could allow the application to specify an increment value or multiplying factor when the queue is instantiated. We could use the original capacity as the increment value.
11 Options for the enlarge method We could set a constant increment value or multiplying factor within the class. We could allow the application to specify an increment value or multiplying factor when the queue is instantiated. We could use the original capacity as the increment value.
12 Options for the enlarge method We could set a constant increment value or multiplying factor within the class. We could allow the application to specify an increment value or multiplying factor when the queue is instantiated. We could use the original capacity as the increment value.
13 Brainstorming The enlarge operation is costly increment by large amount. Increment too much waste of both time & space. Let us use henceforth the original capacity as the increment value: Instantiate an array with a size equal to the current capacity plus the original capacity. Remember the original capacity using an instance variable origicap.
14 Brainstorming The enlarge operation is costly increment by large amount. Increment too much waste of both time & space. Let us use henceforth the original capacity as the increment value: Instantiate an array with a size equal to the current capacity plus the original capacity. Remember the original capacity using an instance variable origicap.
15 Brainstorming The enlarge operation is costly increment by large amount. Increment too much waste of both time & space. Let us use henceforth the original capacity as the increment value: Instantiate an array with a size equal to the current capacity plus the original capacity. Remember the original capacity using an instance variable origicap.
16 Brainstorming The enlarge operation is costly increment by large amount. Increment too much waste of both time & space. Let us use henceforth the original capacity as the increment value: Instantiate an array with a size equal to the current capacity plus the original capacity. Remember the original capacity using an instance variable origicap.
17 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
18 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
19 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
20 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
21 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
22 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
23 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
24 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
25 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origicap; // original capacity protected int numelements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origicap = DEFAULTCAP; public ArrayBoundedQueue(int origicap) { queue = new Object[origiCap]; rear = origicap- 1; this.origicap = origicap;
26 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
27 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
28 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
29 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
30 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
31 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
32 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { // Increments the capacity of the queue by an amount equal to the original capacity. private void enlarge() { // Create a larger array. Object[] larger = new Object[queue.length + origicap]; // Copy the elements from the smaller to the larger array. int currsmaller = front; for (int currlarger = 0; currlarger < numelements; currlarger++){ larger[currlarger] = queue[currsmaller]; currsmaller = (currsmaller+1)%queue.length; // Update the instance variables. queue = larger; front = 0; rear = numelements - 1;
33 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public void enqueue(object element) { if(numelements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numelements = numelements + 1;
34 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public void enqueue(object element) { if(numelements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numelements = numelements + 1;
35 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public void enqueue(object element) { if(numelements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numelements = numelements + 1;
36 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public void enqueue(object element) { if(numelements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numelements = numelements + 1;
37 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public void enqueue(object element) { if(numelements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numelements = numelements + 1;
38 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public void enqueue(object element) { if(numelements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numelements = numelements + 1;
39 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public Object dequeue() { if (isempty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; else { Object itemtoreturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numelements = numelements - 1; return itemtoreturn;
40 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public Object dequeue() { if (isempty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; else { Object itemtoreturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numelements = numelements - 1; return itemtoreturn;
41 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public Object dequeue() { if (isempty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; else { Object itemtoreturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numelements = numelements - 1; return itemtoreturn;
42 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public Object dequeue() { if (isempty()){ System.out.println("Dequeue attempted on an empty queue!"); return null; else { Object itemtoreturn = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numelements = numelements - 1; return itemtoreturn;
43 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public boolean isempty() { return (numelements == 0);
44 Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface public boolean isempty() { return (numelements == 0);
45 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: It does not require elements to be inserted into the rear of the queue. For example, inserted elements might be ordered based on a priority value.
46 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: It does not require elements to be inserted into the rear of the queue. For example, inserted elements might be ordered based on a priority value.
47 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: It extends the Collection interface, so any class that implements it must also implement eight predefined abstract methods (e.g., to observe the size of the queue and to turn the queue into an array).
48 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: No separate interfaces for bounded and unbounded queues exist. Instead, the programmer chooses whether to place a limit on the number of elements a queue holds.
49 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: It provides 2 operations for enqueuing: -add (throws an exception if invoked on a full queue) -offer (returns a boolean value of false if invoked on a full queue).
50 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: It provides two operations for dequeuing: -remove (throws an exception) -poll (returns false, when invoked on an empty queue).
51 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: Operations for obtaining the front element, without removing it, are also included. The Java library Collections Framework includes 9 classes that implement its Queue interface.
52 The Java Library Collection Framework Queue A Queue interface exists in the Java library Collection. This is similar to our Queue ADT interfaces (elements are always removed from the front ). However, it also has several differences: Operations for obtaining the front element, without removing it, are also included. The Java library Collections Framework includes 9 classes that implement its Queue interface.
53 Interesting applications Identifying palindromes. A palindrome is a string that reads the same forward and backward: Madam, I m Adam. Eve Implementing decks in card games
54 Interesting applications Identifying palindromes. A palindrome is a string that reads the same forward and backward: Madam, I m Adam. Eve Implementing decks in card games
55 The List ADT Lists are one of the most widely used ADTs in computer science, which is only natural given how often we use them in daily life. The list of the kinds of lists that we make is endless! With a list, unlike with queues & stacks, we access elements within the structure: check if element is contained, insert name in alphabetical order, delete an entry
56 The List ADT Lists are one of the most widely used ADTs in computer science, which is only natural given how often we use them in daily life. The list of the kinds of lists that we make is endless! With a list, unlike with queues & stacks, we access elements within the structure: check if element is contained, insert name in alphabetical order, delete an entry
57 The List ADT Lists are one of the most widely used ADTs in computer science, which is only natural given how often we use them in daily life. The list of the kinds of lists that we make is endless! With a list, unlike with queues & stacks, we access elements within the structure: check if element is contained, insert name in alphabetical order, delete an entry
58 The List ADT We only need a few variations of the List ADT to represent the majority of realworld lists: unsorted, sorted, indexed Most differences in list types relate to the types of values that they store, rather than to their structure or operations. An important structural difference is how the values are ordered with respect to one another within each type of list. List operations sometimes depend on the content of the list elements let us see how we can compare objects.
59 The List ADT We only need a few variations of the List ADT to represent the majority of realworld lists: unsorted, sorted, indexed Most differences in list types relate to the types of values that they store, rather than to their structure or operations. An important structural difference is how the values are ordered with respect to one another within each type of list. List operations sometimes depend on the content of the list elements let us see how we can compare objects.
60 The List ADT We only need a few variations of the List ADT to represent the majority of realworld lists: unsorted, sorted, indexed Most differences in list types relate to the types of values that they store, rather than to their structure or operations. An important structural difference is how the values are ordered with respect to one another within each type of list. List operations sometimes depend on the content of the list elements let us see how we can compare objects.
61 The List ADT We only need a few variations of the List ADT to represent the majority of realworld lists: unsorted, sorted, indexed Most differences in list types relate to the types of values that they store, rather than to their structure or operations. An important structural difference is how the values are ordered with respect to one another within each type of list. List operations sometimes depend on the content of the list elements let us see how we can compare objects.
62 The equals method The equals method, as defined in the Object class, acts in much the same way as the comparison operator. It returns true if and only if the two variables reference the same object. We can redefine the method within a class to fit the goals of the class. We can then use the method to, e.g., check if a particular element is on a particular list.
63 The equals method The equals method, as defined in the Object class, acts in much the same way as the comparison operator. It returns true if and only if the two variables reference the same object. We can redefine the method within a class to fit the goals of the class. We can then use the method to, e.g., check if a particular element is on a particular list.
64 The equals method The equals method, as defined in the Object class, acts in much the same way as the comparison operator. It returns true if and only if the two variables reference the same object. We can redefine the method within a class to fit the goals of the class. We can then use the method to, e.g., check if a particular element is on a particular list.
65 The equals method int A int B 8 8 int A == int B evaluates to true.
66 The equals method int A int B 8 5 int A == int B evaluates to false.
67 The equals method Circle c1 c1 == c2 evaluates to false. Circle c2
68 The equals method Circle c1 c1 == c2 evaluates to true. Circle c2
69 The equals method public class Circle { int radius; Circle(int someradius){ this.radius = someradius; public boolean equals(circle circle){ if(this.radius == circle.radius) return true; else return false; public static void main(string[] args) { Circle c1 = new Circle(3); System.out.println(c1.radius); Circle c2 = new Circle(3); System.out.println(c2.equals(c1)); We redefine the equals method to fit the goals of the class.
70 Summary The Queue ADT Implementation of unbounded queues The Java Library Collection Framework Queue Introduction to Lists and the equals method Next: The List ADT
CSC 1052 Algorithms & Data Structures II: Queues
CSC 1052 Algorithms & Data Structures II: Queues Professor Henry Carter Spring 2018 Recap Recursion solves problems by solving smaller version of the same problem Three components Applicable in a range
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Queues ArrayQueue Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract These lecture notes are meant to be looked
More informationAnnouncements Queues. Queues
Announcements. Queues Queues A queue is consistent with the general concept of a waiting line to buy movie tickets a request to print a document crawling the web to retrieve documents A queue is a linear
More informationInsertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue
Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,
More informationQueues. CITS2200 Data Structures and Algorithms. Topic 5
CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of
More informationA queue is a linear collection whose elements are added on one end and removed from the other
A queue is consistent with the general concept of a waiting line to buy movie tickets a request to print a document crawling the web to retrieve documents Adding an element Removing an element A queue
More informationLecture Data Structure Stack
Lecture Data Structure Stack 1.A stack :-is an abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards
More informationCSC 1052 Algorithms & Data Structures II: Linked Queues
CSC 1052 Algorithms & Data Structures II: Linked Queues Professor Henry Carter Spring 2018 Recap A queue simulates a waiting line, where objects are removed in the same order they are added The primary
More informationCSCD 326 Data Structures I Queues
CSCD 326 Data Structures I Queues 1 Linked List Queue Implementation Diagram Front Back A B C D 0 6 public interface QueueInterface { Queue Interface public boolean isempty(); // Determines whether
More information1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University
//3 Part Queues CS Algorithms and Data Structures Outline Queue? Implementing Queue Comparison implementations 8 Photo by David Jump 9 //3 "Grill the Buffs" event Queue A queue is like a line of people
More information1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue
Outline Part Queues Queue? Implementing Queue Comparison implementations CS Algorithms and Data Structures 8 "Grill the Buffs" event Photo by David Jump 9 Queue A queue is like a line of people New item
More informationQueue: Queue Representation: Basic Operations:
Queue: Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove
More informationQueue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.
/3/ Outline Queue? Part Queues Implementing Queue Comparison implementations CS Algorithms and Data Structures "Grill the Buffs" event 9/ 3 Photo by David Jump Queue OperaLons A queue is like a line of
More informationIntroduction to Data Structures and Algorithms
Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way. Data Structures
More informationCPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues
CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 is available.
More informationCSE 143 SAMPLE MIDTERM
CSE 143 SAMPLE MIDTERM 1. (5 points) In some methods, you wrote code to check if a certain precondition was held. If the precondition did not hold, then you threw an exception. This leads to robust code
More informationAbstract Data Type: Stack
Abstract Data Type: Stack Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations
More informationStacks and Queues III
EE Data Structures and lgorithms http://www.ecs.umass.edu/~polizzi/teaching/ee/ Stacks and Queues III Lecture 8 Prof. Eric Polizzi Queues overview First In First Out (FIFO) Input D Output Queues allow
More informationStandard ADTs. Lecture 19 CS2110 Summer 2009
Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2
More informationLIFO : Last In First Out
Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing.
More information182 review 1. Course Goals
Course Goals 182 review 1 More experience solving problems w/ algorithms and programs minimize static methods use: main( ) use and overload constructors multiple class designs and programming solutions
More informationCS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type
More informationCS200: Queues. Prichard Ch. 8. CS200 - Queues 1
CS200: Queues Prichard Ch. 8 CS200 - Queues 1 Queues First In First Out (FIFO) structure Imagine a checkout line So removing and adding are done from opposite ends of structure. 1 2 3 4 5 add to tail (back),
More informationQueues COL 106. Slides by Amit Kumar, Shweta Agrawal
Queues COL 106 Slides by Amit Kumar, Shweta Agrawal The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out (FIFO) scheme Insertions are at the rear
More informationCMP Points Total Midterm Spring Version (16 Points) Multiple Choice:
CMP-338 106 Points Total Midterm Spring 2017 Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use
More informationCT 229 Object-Oriented Programming Continued
CT 229 Object-Oriented Programming Continued 24/11/2006 CT229 Summary - Inheritance Inheritance is the ability of a class to use the attributes and methods of another class while adding its own functionality
More informationADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )
ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a
More informationCSEN 301 Data Structures and Algorithms
CSEN 301 Data Structures and Algorithms Lecture 5: Queues: Implementation and usage Prof. Dr. Slim Abdennadher slim.abdennadher@guc.edu.eg German University Cairo, Department of Media Engineering and Technology
More informationQueues. Queue ADT Queue Implementation Priority Queues
Queues Queue ADT Queue Implementation Priority Queues Queue A restricted access container that stores a linear collection. Very common for solving problems in computer science that require data to be processed
More informationCPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues
CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 available very
More informationcsci 210: Data Structures Stacks and Queues
csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks
More informationAshish Gupta, Data JUET, Guna
Introduction In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station etc. The person who come first, he/she
More informationUNIT-2 Stack & Queue
UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards
More informationCSE 373 Data Structures and Algorithms. Lecture 2: Queues
CSE 373 Data Structures and Algorithms Lecture 2: Queues Queue ADT queue: A list with the restriction that insertions are done at one end and deletions are done at the other First-In, First-Out ("FIFO
More informationCSE 214 Computer Science II Heaps and Priority Queues
CSE 214 Computer Science II Heaps and Priority Queues Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction
More informationPlease note that if you write the mid term in pencil, you will not be allowed to submit a remark request.
University of Toronto CSC148 Introduction to Computer Science Fall 2001 Mid Term Test Section L5101 Duration: 50 minutes Aids allowed: none Make sure that your examination booklet has 8 pages (including
More informationMidterm Exam (REGULAR SECTION)
Data Structures (CS 102), Professor Yap Fall 2014 Midterm Exam (REGULAR SECTION) October 28, 2014 Midterm Exam Instructions MY NAME:... MY NYU ID:... MY EMAIL:... Please read carefully: 0. Do all questions.
More informationData Structure - Stack and Queue-
Data Structure - Stack and Queue- Hanyang University Jong-Il Park STACK Stack ADT List that insertions and deletions can be performed at the end of the list Operations Push(X, S): insert X in the list
More informationPostfix (and prefix) notation
Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator
More informationAssignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.
Assignment 1 Check your mailbox on Thursday! Grade and feedback published by tomorrow. COMP250: Queues, deques, and doubly-linked lists Lecture 20 Jérôme Waldispühl School of Computer Science McGill University
More information1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures
1.00 Lecture 26 Data Structures: Introduction Stacks Reading for next time: Big Java: 19.1-19.3 Data Structures Set of primitives used in algorithms, simulations, operating systems, applications to: Store
More informationInterfaces & Generics
Interfaces & Generics CSC207 Winter 2018 The Programming Interface The "user" for almost all code is a programmer. That user wants to know:... what kinds of object your class represents... what actions
More informationECE 242 Data Structures and Algorithms. Linked List I. Lecture 10. Prof.
ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Linked List I Lecture 10 Prof. Eric Polizzi Logistics ECE242 Recommendations The goal of projects is to help you
More informationCH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES
CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER
More informationCS24 Week 4 Lecture 2
CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track
More informationCSC 1052 Algorithms & Data Structures II: Stacks
CSC 1052 Algorithms & Data Structures II: Stacks Professor Henry Carter Spring 2018 Recap Abstraction allows for information to be compartmentalized and simplifies modular use Interfaces are the Java construction
More informationKeeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10
Keeping Order:! Stacks, Queues, & Deques 1 Stacks 2 Stacks A stack is a last in, first out (LIFO) data structure Primary Operations: push() add item to top pop() return the top item and remove it peek()
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant
More information1 P age DS & OOPS / UNIT II
UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular
More informationOverview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting
Overview ITI 1121. Introduction to Computing II Rafael Falcon and Marcel Turcotte (with contributions from R. Holte) Electrical Engineering and Computer Science University of Ottawa Interface Abstract
More informationHow can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.
Queues 1 Queues A Queue is a sequential organization of items in which the first element entered is the first removed. They are often referred to as FIFO, which stands for first in first out. Examples:
More informationQueues. Stacks and Queues
Queues Reading: RS Chapter 14 Slides are modified from those provided by Marty Stepp www.buildingjavaprograms.com 1 Stacks and Queues Sometimes a less powerful, but highly optimized collection is useful.
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 5: Stacks and Queues 2017 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:
More informationData Abstraction and Specification of ADTs
CITS2200 Data Structures and Algorithms Topic 4 Data Abstraction and Specification of ADTs Example The Reversal Problem and a non-adt solution Data abstraction Specifying ADTs Interfaces javadoc documentation
More informationLists. CITS2200 Data Structures and Algorithms. Topic 9
CITS2200 Data Structures and Algorithms Topic 9 Lists Why lists? List windows Specification Block representation Singly linked representation Performance comparisons Reading: Lambert and Osborne, Sections
More information1. Introduction. Lecture Content
Lecture 6 Queues 1 Lecture Content 1. Introduction 2. Queue Implementation Using Array 3. Queue Implementation Using Singly Linked List 4. Priority Queue 5. Applications of Queue 2 1. Introduction A queue
More information1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University
Chapter 5: Stacks, ueues and Deques Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
More informationLists. ordered lists unordered lists indexed lists 9-3
The List ADT Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations Compare list implementations 9-2 Lists A list is a
More information" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)
/7/ Queue vs Priority Queue Priority Queue Implementations queue 0 0 Keep them sorted! (Have we implemented it already?) Appropriate if the number of items is small Sorted Array-based items 0... items
More informationPriority Queues. 04/10/03 Lecture 22 1
Priority Queues It is a variant of queues Each item has an associated priority value. When inserting an item in the queue, the priority value is also provided for it. The data structure provides a method
More informationLECTURE OBJECTIVES 6-2
The Queue ADT LECTURE OBJECTIVES Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations
More informationStack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea
Stack as an ADT COMP 182: Data Structures and Program Design Lecture Slides 8 John Noga CSU Northridge April 9, 200 public interface Stack { public boolean push(ourthing ot); public OurThing (); public
More informationCOMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
More informationData Structures G5029
Data Structures G5029 Lecture 2 Kingsley Sage Room 5C16, Pevensey III khs20@sussex.ac.uk University of Sussex 2006 Lecture 2 Stacks The usual analogy is the stack of plates. A way of buffering a stream
More informationSearch EECS /57
1/57 Search EECS 4315 www.eecs.yorku.ca/course/4315/ Source: weknowyourdreams.com 2/57 Breadth first search 3/57 import gov.nasa.jpf.search.search; public class BFSearch extends Search {... Constructor
More informationAnother common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties
Queues FIFO queue ADT Examples using queues reading character string in order recognize palindromes Queue implementations LL pointer based List ADT based array based tradeoffs 1 ADT queue operations Create
More informationStacks, Queues (cont d)
Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1
More informationApply to be a Meiklejohn! tinyurl.com/meikapply
Apply to be a Meiklejohn! tinyurl.com/meikapply Seeking a community to discuss the intersections of CS and positive social change? Interested in facilitating collaborations between students and non-profit
More informationQueues. Data Structures and Design with Java and JUnit Rick Mercer 18-1
Queues Data Structures and Design with Java and JUnit Rick Mercer 18-1 A Queue ADT First in first out access A Queue is another name for waiting line Example: Submit jobs to a network printer What gets
More informationCSE 143 SAMPLE MIDTERM SOLUTION
CSE 143 SAMPLE MIDTERM SOLUTION 1. (5 points) In some methods, you wrote code to check if a certain precondition was held. If the precondition did not hold, then you threw an exception. This leads to robust
More information== isn t always equal?
== isn t always equal? In Java, == does the expected for primitives. int a = 26; int b = 26; // a == b is true int a = 13; int b = 26; // a == b is false Comparing two references checks if they are pointing
More informationBasic Data Structures
Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1
More informationQueues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff
Queues Virtuelle Fachhochschule Prof. Dr. Debora Weber-Wulff!1 Queues First In, First Out Well-known in socialist society Operations enqueue join the back of the line dequeue remove from the front of the
More informationProblem 1: Building and testing your own linked indexed list
CSCI 200 Lab 8 Implementing a Linked Indexed List In this lab, you will be constructing a linked indexed list. You ll then use your list to build and test a new linked queue implementation. Objectives:
More informationCS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 SPRING 2019 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due Thursday night (14 February) Go back to Lecture 6 & discuss method
More informationCMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012
CMPSCI 187: Programming With Data Structures Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012 Concurrency and a Case Study Concurrency and Threads Example: Counter, Increase,
More informationAbstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011
CS/ENGRD 110 Object-Oriented Programming and Data Structures Spring 011 Thorsten Joachims Lecture 16: Standard ADTs Abstract Data Types (ADTs) A method for achieving abstraction for data structures and
More informationCSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:
CSE 326 Team CSE 326: Data Structures Instructor: Steve Seitz TAs: Winter 2009 Steve Seitz Lecture 1 Eric McCambridge Brent Sandona Soyoung Shin Josh Barr 2 Today s Outline Introductions Administrative
More informationINFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues
INFO1x05 Tutorial 6 Heaps and Priority Queues Exercise 1: 1. How long would it take to remove the log n smallest elements from a heap that contains n entries, using the operation? 2. Suppose you label
More informationDefinition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.
5 Linked Structures Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the
More informationBasic Data Structures 1 / 24
Basic Data Structures 1 / 24 Outline 1 Some Java Preliminaries 2 Linked Lists 3 Bags 4 Queues 5 Stacks 6 Performance Characteristics 2 / 24 Some Java Preliminaries Generics (aka parametrized types) is
More informationSubject : Computer Science. Paper: Data Structures. Module: Priority Queue and Applications. Module No: CS/DS/14
e-pg Pathshala Subject : Computer Science Paper: Data Structures Module: Priority Queue and Applications Module No: CS/DS/14 Quadrant 1- e-text Welcome to the e-pg Pathshala Lecture Series on Data Structures.
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) An interesting definition for stack element The stack element could be of any data type struct stackelement int etype; union int ival;
More informationCE 221 Data Structures and Algorithms
CE 2 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, 6.1 6.3 Izmir University of Economics 1 A kind of queue Priority Queue (Heap) Dequeue gets element with the
More informationObject Oriented Programming in C#
Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create
More informationQueues. Queue ADT. Queue Specification. An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements.
Queues Look!up tables Hash tables An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements. Maps When dequeuing, the element that has been in the queue
More informationQueue rear tail front head FIFO
The Queue ADT Objectives Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis The ADT Queue Like a line of people First person in line is first person served New element of queue enter at its back
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation
More informationPriority Queue. How to implement a Priority Queue? queue. priority queue
Priority Queue cs cs cs0 cs cs cs queue cs cs cs cs0 cs cs cs cs cs0 cs cs cs Reading LDC Ch 8 priority queue cs0 cs cs cs cs cs How to implement a Priority Queue? Keep them sorted! (Haven t we implemented
More informationLecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University
Lecture 6 COMP1006/1406 (the OOP course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments A1,A2,A3 are all marked A4 marking just started A5 is due Friday, A6 is due Monday a quick
More informationSubclassing for ADTs Implementation
Object-Oriented Design Lecture 8 CS 3500 Fall 2009 (Pucella) Tuesday, Oct 6, 2009 Subclassing for ADTs Implementation An interesting use of subclassing is to implement some forms of ADTs more cleanly,
More informationEXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
T E W H A R E W Ā N A N G A O T E Student ID:....................... Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 2 COMP103 INTRODUCTION
More informationCS61B Spring 2016 Guerrilla Section 2 Worksheet
Spring 2016 27 February 2016 Directions: In groups of 4-5, work on the following exercises. Do not proceed to the next exercise until everyone in your group has the answer and understands why the answer
More informationLab 11. A sample of the class is:
Lab 11 Lesson 11-2: Exercise 1 Exercise 2 A sample of the class is: public class List // Methods public void store(int item) values[length] = item; length++; public void printlist() // Post: If the list
More informationAn Introduction to Queues With Examples in C++
An Introduction to Queues With Examples in C++ Prof. David Bernstein James Madison University Computer Science Department bernstdh@jmu.edu Motivation Queues are very straightforward but are slightly more
More informationName CPTR246 Spring '17 (100 total points) Exam 3
Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in
More informationEXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 6: Stacks and Queues 2015-2016 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:
More informationLinear Data Structures
Linear Data Structures Arrays Arrays are stored in contiguous memory locations and contain similar data An element can be accessed, inserted or removed by specifying its position (number of elements preceding
More information