COMP 213
Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)
COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue - - PowerPoint PPT Presentation
COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) 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:
Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)
the opposite end is called a queue.
client.
not depend on the boundedness of the queue.
extend QueueInterface.
UnboundedQueueInterface.
structure into the new array.
UnboundedQueueInterface.
structure into the new array.
UnboundedQueueInterface.
structure into the new array.
space.
if (numElements == queue.length) enlarge();
space.
if (numElements == queue.length) enlarge();
space.
if (numElements == queue.length) enlarge();
class.
factor when the queue is instantiated.
class.
factor when the queue is instantiated.
class.
factor when the queue is instantiated.
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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;
} public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }
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; } ... }
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; } ... }
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; } ... }
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; } ... }
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; } ... }
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; } ... }
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; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public void enqueue(Object element) { if(numElements == queue.length) enlarge(); rear = (rear+1)%queue.length; queue[rear] = element; numElements = numElements + 1; } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override 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; } } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override 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; } } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override 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; } } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override 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; } } ... }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public boolean isEmpty() { return (numElements == 0); } }
public class ArrayUnbQueue implements UnboundedQueueInterface { ... @Override public boolean isEmpty() { return (numElements == 0); } }
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:
example, inserted elements might be ordered based on a priority value.
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:
example, inserted elements might be ordered based on a priority value.
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:
also implement eight predefined abstract methods (e.g., to observe the size
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:
the programmer chooses whether to place a limit on the number of elements a queue holds.
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:
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:
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:
included.
The Java library Collections Framework includes 9 classes that implement its Queue interface.
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:
included.
The Java library Collections Framework includes 9 classes that implement its Queue interface.
A palindrome is a string that reads the same forward and backward:
A palindrome is a string that reads the same forward and backward:
structure:
structure:
structure:
world lists:
than to their structure or operations.
how we can compare objects.
world lists:
than to their structure or operations.
how we can compare objects.
world lists:
than to their structure or operations.
how we can compare objects.
world lists:
than to their structure or operations.
how we can compare objects.
way as the comparison operator. It returns true if and only if the two variables reference the same object.
particular list.
way as the comparison operator. It returns true if and only if the two variables reference the same object.
particular list.
way as the comparison operator. It returns true if and only if the two variables reference the same object.
particular list.
int A int B “int A == int B” evaluates to true.
int A int B “int A == int B” evaluates to false.
Circle c1 Circle c2 “c1 == c2” evaluates to false.
Circle c1 Circle c2 “c1 == c2” evaluates to true.
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.