牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

スタックとキューを Python でやってみた

スタックとキューの動き?がよくわからなかったのでPythonでやってみた

#stack and que
#http://www.yt3.info/data_structure/stack_and_queue.html
str = ['a', 'b', 'c']
print "##### STACK"
stack = []
print "----------Push"
for s in str:
    print s
    stack.append(s)
    print stack
print "----------Pop"
for s in stack + []:  #Why should add the [] to sequence?
    print stack.pop()
    print stack

print"##### QUEUE"
queue = []
print "----------Enqueue"
for s in str:
    print s
    queue.append(s)
    print queue
print "----------Dequeue"
for s in queue + []:
    print queue.pop(0) # pop(0) zero!
    print queue

結果

##### STACK
----------Push
a
['a']
b
['a', 'b']
c
['a', 'b', 'c']
----------Pop
c
['a', 'b']
b
['a']
a
[]
##### QUEUE
----------Enqueue
a
['a']
b
['a', 'b']
c
['a', 'b', 'c']
----------Dequeue
a
['b', 'c']
b
['c']
c
[]

こんな感じ?

スタックが尻入れ・尻出しで、キューが尻入れ・頭出し。