Sunday, 8 February 2015

Week 5: the stack and queue.

This week we learned the class Stack and class Queue. These two classes actually are two way to store and remove elements of list or string.

For class Stack: we store things like filling something into a bottle, so the element added last was pulled out first. In other words, remove the element at the end of the list or string.

For class Queue: we store things like lining up a line so that the element added first was pulled first. In other words, remove the element at the beginning of the list or string.

In Lab#4, we have the exercise with the Stack and Queue. I think the hard part is storing the list in right arrangement so that the output of methods matches the requirements.Firstly, I used recursion function I learned last week to transform the nested list ([1, [2, 3], 4]) in to a simple list ([1, 2, 3, 4]). Then, we can use the simple list to be stored by Stack.push(). Similarly, we also need change the format of the input when we use the class queue. Secondly, we use input('Type...:') and while loop with __.is empty for both class to store the string and integer for class Stack and class Queue respectively.

I feel these two classes will be very useful in the future, let's see!

No comments:

Post a Comment