As promised, my second post about ADT will be an indepth analysis of and ADT covered in class. In my previous post, I talked about ADT as a specification manual of some sort for some general object. At the end, ADT allowed us to be modular. There are several very useful abstract datatypes we covered in class. In addition to some of the built-in types such as integers, lists, etc, we covered several other ways to store data: trees, queue, and stack.
For no good reason, I want to focus on queues. Queues are first in first out structures. It may have enqueue, dequeue, and is_empty. Think about queues in a store. You're waiting to get your icecream on a hot summer day. If you come first, you can get your icecream first. It doesn't matter to the icecream seller who exactly is in position 2. All that matters is that the queue isn't empty, and the next person who gets icecream. When that person gets the icecream, they are removed from the queue
So translating the icecream stand using the concept of queues we get something like the following:
is_empty? Yes
is_empty? Yes
someone came, enqueue Jayce
someone came, enqueue Katarina
is_empty? No
dequeue... Jayce
serving Jayce icecream
someone came, enqueue Corki
still serving Jayce icecream
Jayce got icecream
is_empty? No
dequeue ... Katarina
serving Katarina icecream
still serving Katarina icecream
Katarina got icecream
is_empty? No
dequeue ... Corki
serving Corki icecream
still serving Corki icecream
Corki got icecream
is_empty? Yes
is_empty? Yes
... and so forth.
This doesn't apply just in the context of an icecream stand. Of course any line in a store can be used, but it's more than just that.
A less physical example is when requests are processed in the computer. For example, the computer seems to run a lot of programs simultaneously. In actuality, these are processed by a queue. Say you need to do X, Y, Z "simultaneously". You can create the illusion of simultaneous action by doing X, Y, Z, X, Y, Z, ... so forth but changing between them really quickly. It's kind of similar to flashing bird and cage to create bird in cage. Rapidly switching between tasks can make it seem like they are occuring simultaneously. In the context of a queue, we can think of X, Y, Z as being part of the queue. dequeue X and do X at the end enqueue X and then dequeue Y... and so forth.
The same basic data structure can be applied to many different "specific" cases since it's an Abstract Data Type.
No comments:
Post a Comment