Our Storage mechanism is as follows: // _underscores indicate "private variables" to other engineers const Queue = function(capacity) q.until('bb') // 2q.count() // 2 The difference between the two gives the queue size. To track our items, we use the head for the front of the queue and tail for the back. Other methods include peek, contains, until, and count. When an item is inserted into a queue, it’s called enqueued. Then we’ll start with an implementation using the pseudoclassical method and a base object. Like my post on Stacks, we will describe the API for a Queue. ImplementationĪ simple implementation using arrays is with the method shift() to remove from the front and unshift() to add the front. In Queues, items are processed in the order they are inserted. Each item in the array gets processed in the order it was inserted, from index 0 to index.length - 1. This also happens when we iterate over items with a for or while loop, forEach(), or map() method. The main reason is queues process data fairly and preserve the order of the collection. What would happen if we used a LIFO, or stack data structure? For simplicity sake, let’s say we need to capitalize every letter streamed. As it comes in, we need to do something to it and then write it to a file to read later. For example, suppose we have a stream of data in the node. So we have differences in the order of processing - why? We need a different method of processing data that preserves the order. Queues are very similar to Stacks regarding interface, with the difference being Stacks process data Last In, First Out. This method of ordering data for service, in our case, people, is what Queues are all about. Other real-life scenarios are toll booths or wedding chapels in Vegas. The customers are arranged in a particular order, First In, First Out. When you go the Shake Shack, most often there are other people on the line waiting to be served. By Kevin Turney Data Structures 101: Queues credit: att systems group Starting with a Queue
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |