* * Robert Sedgewick * Kevin Wayne */ public class ResizingArrayQueue implements Iterable Ĭopyright © 2000–2022, Robert Sedgewick and Kevin Wayne. * * For additional documentation, see Section 1.3 of * Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. For the enqueue operation, check if the queue is full i.e rear N-1. Declare two variables front and rear and initialize them with -1. * The size, peek, and is-empty operations takes * constant time in the worst case. Queue using array Algorithm : Create an array queue of size N. * The enqueue and dequeue operations take constant amortized time. Now if the assignment operator fails on the dequeue for some odd reason (memory etc). Consider the code: 1.Item i (q.dequeue ()) 2.Item j 3.j s.dequeue () Item i gets initialized with dequeue value. * * This implementation uses a resizing array, which double the underlying array * when it is full and halves the underlying array when it is one-quarter full. Herb and Sutter explains this very well with a stack implementation. ****************************************************************************** * Compilation: javac ResizingArrayQueue.java * Execution: java ResizingArrayQueue enqueue and dequeue * operations, along with methods for peeking at the first item, * testing if the queue is empty, and iterating through * the items in FIFO order. But once if queue becomes full and later if elements are dequeued, we have to. ResizingArrayQueue.java ResizingArrayQueue.javaīelow is the syntax highlighted version of ResizingArrayQueue.java In a normal Queue Data Structure, we can insert elements until queue becomes full.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |