More Travel Tips:
BBC Asteroid Impact Simulation End Of The World Predictions
A huge asteroid with a diameter of 500 Kilometer is going to impact on Earth! Destination: Pacific Ocean. The asteroid impact peels 10 kilometers crust off the surface of Earth. The resulting shockwave travels at hypersonic speed. The debris from the impact is blasted to the lower orbit of Earth, and they returns to destroy everything on the planet. The firestorm created by the asteroid impact encircles the Earth,
vaporizing everything in our world. Within 24 hours of asteroid impact, the entire Earth will be uninhabitable. The researchers have found that this scenario has happened 6 times in Earth's history.
Queue Chris Gerber, Harvard University This is CS50, CS50.TV One useful data structure for storing an ordered collection of elements is a queue. It is used when the elements need to be removed in the same order as they were added. This concept is referred to as FIFO, which is an acronym for first in, first out. To help visualize this, it may be useful to picture
a checkout line at a store. As people arrive, they wait at the back of the line. The cashier then takes turns serving the customers at the front, who then exit the line one at a time. In computer science, we refer to the front of the queue as the head and the back as the tail. An example of when we might use this in an application is a waitlist for class enrollments.
As seats become available in the class, the person at the head of the waiting list is provided the opportunity to enroll in the class. gt;gt; A queue can be constructed using any collection that stores data in order, such as an array or a linked list. Along with the collection to store the items in the queue, we also need a method to add items at the end of the queue, which is called enqueuing, and another to remove an item from the head of the queue,
which is called dequeuing. It is often useful to include another method to return the current length of the queue as well as a method to check if the queue is empty. Let's look at how we might implement a queue of integers in C, using an array for the collection of elements. First, we create a structure called queue to hold our variables. We will use a fixedsize 0 index array of integers to store the elements.
We will also include a variable called head that stores the index of the element that is currently at the head of the queue. A third variable, called length, will be used to keep track of the number of elements in the array. As an alternative, you could consider using a variable called tail to point to the last field element in the array. Before we write any more code, let's try out our design.
Let's start with an empty array of length 0, with the head set to 0. Now let's enqueue 4 values6, 2, 3, and 1. The length will now be 4, and the head will stay at 0. gt;gt; What happens if we dequeue a valueé We will reduce the length to 3, set the head to 1,
DOUG LLOYD: So if you'vewatched the tutorial on stack, this is probably going to feellike a little bit of deja vu. It's going to a very similar concept,just with a slight twist on it. We're going to talk now about queues. So a queue, similar to a stack,is another kind of data structure that we can use to maintaindata in an organized way. Similar to a stack,it can be implemented
as an array or a linked list. Unlike a stack, the rulesthat we use to determine when things get added and removed froma queue are a little bit different. gt;gt; Unlike a stack, whichis a LIFO structure, last in, first out, a queue is a FIFOstructure, FIFO, first in, first out. Now queues, you probablyhave an analogy to queues. If you've ever been in line atan amusement park or at a bank, there's sort of a fairnessimplementing structure.
The first person in line atthe bank is the first person who gets to speak to the teller. gt;gt; It would be sort of a raceto the bottom if the only way you got to speak to the teller at thebank was to be the last person in line. Everybody would always wantto be the last person in line, and the person who was there firstwho has been waiting for a while, could be there for hours,and hours, and hours before they have a chance to actuallywithdraw any money at the bank.
And so queues are sort of thefairness implementing structure. But that doesn't necessarily meanthat stacks are a bad thing, just that queues are another way to do it. So again a queue is first in, firstout, versus a stack which last in, first out. Similar to a stack,we have two operations that we can perform on queues. The names are enqueue, which is to adda new element to the end of the queue,
and dequeue, which isto remove the oldest element from the front of the queue. So we're going to add elementsonto the end of the queue, and we're going to remove elementsfrom the front of the queue. Again, with the stack, we were addingelements to the top of the stack and removing elementsfrom the top of the stack. So with enqueue, it's adding tothe end, removing from the front. So the oldest thing in thereis always the next thing
to come out if we tryand dequeue something. gt;gt; So again, with queues, we canarraybased implementations and linkedlist based implementations. We'll start again witharraybased implementations. The structure definitionlooks pretty similar. We have another arraythere of data type value, so it can hold arbitrary data types. We're again going to useintegers in this example.