Giter Club home page Giter Club logo

queues's Introduction

Data Structures: Queues

Why is this important?

This workshop is important because:

Data structures are ways of organizing data that model real-world information movement. A queue is built to simulate any situation where people, tasks, or items get in a line and are addressed one by one in the order they arrived; items are addressed in a first in, first out basis. This is important because it's extremely common in the real world.

What are the objectives?

After this workshop, developers will be able to:

  • Describe a queue by its methods and first in, first out (FIFO) behavior.
  • Build queue methods using linked list methods.
  • Compare and contrast stacks and queues and appropriately choose which is better for a given situation.

Where should we be now?

Before this workshop, developers should already be able to:

  • Describe a stack and build its methods
  • Write a Ruby class.

Warmup

  1. What is a stack and what behaviors does it have?
  2. Why were we using playing cards during the stacks lesson?
  3. How would you implement a stack with an array (rather than a linked list)? How would you decide where the top of the stack would be? How would you push something to top of the stack? How would you pop something off the top of the stack?

Queues

queue image with enqueue and dequeue

You can read about them below or watch this video.

Queues as a data structure are a lot like queues as a physical structure. This makes more sense with British English, where queue means "a line" or "to line up". Think of an orderly line of people waiting to do something. We can remove an item from the front of the queue when its turn comes (by poping, also known as dequeueing), or add an item to the back of the queue when it joins the line (by pushing or enqueueing it). Again, while we may be able to "cut" in line in the real world, the queue data structure only allows us to add to the end of the stack or remove from the beginning. The enqueue and dequeue operations for a queue are both O(1) - constant time. Like with stacks, some implementations of queues also allow us to peek at the value of the first item without dequeueing it.

Queues are "First In, First Out" -- the first item enqueued will be the first to move all the way through the line and get dequeued.

stick figure demon eats person cutting in line -- from popcoaster.com

Thinking with Queues

  1. What are some real life structures and objects that a queue could simulate?

  2. Draw a queue after each of the following operations:

  • ENQUEUE 0
  • DEQUEUE
  • ENQUEUE 2
  • ENQUEUE 4
  • ENQUEUE 6
  • DEQUEUE
  • ENQUEUE 8
click for answer... ``` * start [] * ENQUEUE 0 [0] * DEQUEUE [] * ENQUEUE 2 [2] * ENQUEUE 4 [2, 4] * ENQUEUE 6 [2, 4, 6] * DEQUEUE [4, 6] * ENQUEUE 8 [4, 6, 8] ```
  1. How would you implement a queue with an array? Where would you decide the front of the queue would be? How would you enqueue something to the end of the queue? How would you dequeue something from the front of the queue?
super stuck? click for an answer... > The "front" could be the beginning of the array. To enqueue, you'd use JavaScript's handy `push` array method. To dequeue, you could use JavaScript's `shift` method, which removes and returns the first element from an array.
  1. How would you implement a queue with a linked list? Where would you decide the front of the queue would be? How would you enqueue something to the end of the queue? How would you dequeue something from the front of the queue?
super stuck? click for an answer... > The "front" could be the head of the linked list. The "back" could be the tail. You could enqueue by `append`ing to the tail. You could dequeue by deleting and returning the head node.
  1. Stretch: How would you implement a queue with a fixed-size array?

Challenges

  1. Design Decisions Would you use a stack or a queue to...
  • ... print out a list of instructions that must be done in order?

  • ... allow a user to undo changes to a document, one by one?

  • ... let users create playlists and play back the songs?

  • ... display only the 10 most recent messages a user posted, in the order they were posted?

  1. Message Queues

Queues are often used to create "buffers" that temporarily store data from one part of a program until another part of a program can process the data. This is common with asynchronous data transfer, or mismatches between how often data is sent and how often it can be processed.

Think of a scenario where restaurant diners order food faster than the chefs can cook it.

Describe how you would use a queue help the chef keep track of meals to make. What should the chef do when the queue is empty?

  1. Stretch: Try out this queue challenge to calculate the total price of a purchase.

queues's People

Contributors

cofauver avatar

Watchers

James Cloos avatar Sherie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.