Thursday, February 25, 2016

Anyone hungry?

So, I was almost done with chapter 8. It was just a few more collections that act similar to lists. They are queues and stacks. The book used language I was used to hearing in my prior job, LIFO and FIFO (last in first out and first in first out). LIFO refers to stacks and FIFO refers to queues. I thought about those and how I choose how to treat my lots when buying stocks, do I want to pay taxes on those I first bought when I sell or those that were the most recent purchase. Okay, an easier way to think about it queues: treated like a line, the people who get in line first are the first to get their food or be seated. Stacks are take the top, like Pringles. You have to take the top Pringle in the can to get to the ones at the bottom.

Alright, seems simple. Let's take a look.To create a queue you add some code like:

Queue myQueue = new Queue();

Then to add to the queue:

myQueue.Enqueue("Piece of data");

Or remove:

myQueue.Dequeue;

Remember FIFO. That "Piece of data" we put in will be the first out. And now to show stacks:

Stack myStack = new Stack();

A little different to put in a stack, we push the piece of data in:

myStack.Push("Stack data goes here");

Then to remove data from the stack we pop it out:

myStack.Pop;

Both stack and queue inherit from IEnumerable and, pretty easily, we can move our data from a queue to a stack to a list and back (probably any combination you can think of) by overloading the constructor. Example:

Queue myQueue = new Queue();
*enter data into the myQueue*
Stack myStack = new Stack(myQueue);
List myList = new List(myStack);

At the end of a chapter we created a flap jack tracker to see and feed lumberjacks (does anyone like soggy flapjacks?). A lot of concepts are starting to come together.



Next up is another lab. No help on this one, wish me luck!

No comments: