Stack and Queue
1. Stack and Queue are data structures used not only to contain data/elements but more to operate data/elements in a per-specified manner. Even there are various ways to implement stack and queue, the built_in List is usually borrowed to create stack and queue in Python. This is why stack and queue are called adaptive data structure. With the same implementation, stack and queue are significantly different on how to access to and remove data contained. Stack accesses to and removes the data that are latest appended to (i.e., the data on the top), which is called 'Last in First Out' (LIFO). But Queue accesses to and remove the data that are earliest appended to (i.e, the data at the bottom), which is called "First In First Our"(FIFO). Notice both stack and queue append data to the list because they use list to store data.
2. Stack and queue makes it easy to operate data just based on the timing sequence without concerning the data itself. Let's see the examples below.
a. Stack: evaluate the post-fix notation
As an alternative to infix notation like '5 * 7 + 2', a post-fix notation like '5 7 * 2 +' has the nice property that parentheses are unnecessary. Stack is pretty helpful to evaluate the post-fix notation. When '5 7 * 2 +' is evaluated from left to right, 5 and 7 are added to the stack: [5,7]. Next, the operator '*' is applied to 5 and 7 that are removed from the stack and the result (7*5 = 35) is added to the stack: [35]. 2 is then add to stack: [35, 2]. '+' is applied to 35 and 2 that are removed from the stack and the result (2 + 35 =37) is added to the stack. Finally the stack just contain the final accumulated result: [37]. So the key is that the stack contains the numbers that are right before an operator. After the operator is applied to the numbers, the stack contains the accumulated result and then next new numbers.
class Stack():
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items)
def add(self, item):
self.items.append(item)
def remove(self):
return self.items.pop()
def postfix(postfix_str):
token_list = postfix_str.split()
stack = Stack()
for item in token_list:
if item == '+':
result = stack.remove() + stack.remove()
stack.add(result)
elif item == '*':
result = stack.remove() * stack.remove()
stack.add(result)
elif item =="-":
result = stack.remove() - stack.remove()
stack.add(result)
elif item == '/':
first = stack.remove()
second = stack.remove()
result = second / first
stack.add(result)
else:
stack.add(int(item))
return stack.remove()
b. Queue: queue a list that may contain sub-list(s)
First of all, we use a queue to store the list items like [1,[2,3],4]. Next the item is removed from the queue. If the item is non-list, just print it like 1, 4. If it is a sub-list, the items within are added to the queue as non-list items and then re_removed and printed like 2, 3. Notice that the sub-list items are appended to the queue. So they are not removed untill all non-list items are gone.
class Queue():
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def add(self, item):
self.items.append(item)
def remove(self):
return self.items.pop(0)
def queue_list(lst):
queue = Queue()
for item in lst:
queue.add(item)
while not queue.is_empty():
item = queue.remove()
if isinstance(item, list):
for i in item:
queue.add(i)
else:
print(item)
End of the post