วันพฤหัสบดีที่ 18 สิงหาคม พ.ศ. 2559

Building Software : stack

Code :

class Stack:
  def __init__(self):
    self.item = []

  def isEmpty(self):
    return self.item == []

  def push(self,item):
    self.item.append(item)

  def pop(self):
    return self.item.pop()

  def size(self):
    return len(self.item)


def setup():
  a = Stack()
  print a.isEmpty()
  a.push(1)
  a.push(2)
  print a.pop()
  print 'size is',a.size()
  a.push(7)
  a.push(8)
  a.push(10)
  a.pop()
  while(a.isEmpty() != True):
    print a.pop()

setup()

..................................................................................................................

result :

xxxxxx:~/ดาวน์โหลด/Work$ python stack.py
True
2
size is 1
8
7
1





ไม่มีความคิดเห็น:

แสดงความคิดเห็น