Stack---python

# -*-coding: utf-8 -*-
# Author :writen by Qiusheng Li
# Time: 2014/08/15

# push Insert an item at the top of the stack
# pop Remove an item from the top of the stack
# peek Look up the element on the top
# empty/size Check whether the stack is empty or return its size
# How to implement with lists and its methods append() and pop()

class StackBefore:
      '''define the stackbefore class'''
      def __init__(self,a,b):
           self.r = a
           self.i = b
      kind = 'struction'  # all class variable shared by all instances
      def add(self,x):    # add self.r and self.i as argument add argument x
            return self.i + self.r + int(x)  # return total result
''' testing code
      c = StackBefore(2,3)
      d = StackBefore(3,4)
      print c.r,c.i,c.kind
      print d.r,d.i,d.kind
      print 'c : add() ',c.add(2) 
      print 'd : add() ',d.add(2)
      print 'OK'

'''

'''testing result
   2 3 struction
   3 4 struction
   c : add()  7
   d : add()  9
   OK
'''
# first coding Stack(using list)
class Stack:
      '''define the stack class'''
      def __init__(self):
            self.items = []
            
      def push(self,items):
            self.items.append(items)
            
      def isEmpty(self):
            return self.items == []
      
      def pop(self):
            if not self.isEmpty():
                  return self.items.pop()
            else:
                  raise Exception('empty!')
            
      def top(self):
            if not self.isEmpty():
                  return self.items[-1]
            else:
                  raise Exception('empty!')
      
      def size(self):
            return len(self.items)

def StackMain():
         stack = Stack()
         stack.push(1)
         stack.push(2)
         stack.push(3)
         print 'size ',stack.size()
         print 'top ',stack.top()
         print 'pop ',stack.pop()
         print 'pop ',stack.pop()
         print 'pop ',stack.pop()
         print 'size ',stack.size()


#second coding Stack(using myself data struction)
class Node(object):
      def __init__(self,value=None):
            self.value = value
            self.next = None


class StackNode(object):
      '''define the StackNode class'''
      def __init__(self):
            self.top = None
            
      def push(self,value):
            node = Node(value)
            node.next = self.top
            self.top = node

      def isEmpty(self):
            return not bool(self.top)
      
      def pop(self):
            node = self.top
            if node:
               self.top = node.next
               return node.value
            else:
                  raise Exception('StackNode is empty!')
            
      def peek(self):
            if bool(self.top):
                  return self.top.value
            else:
                  raise Exception('StackNode is empty!')
            
      def size(self):
         node = self.top
         i = 0
         while node:
               i += 1
               node = node.next
         return i
#testing code
def StackNodeMain():
         stack = StackNode()
         stack.push(1)
         stack.push(2)
         stack.push(3)
         print 'size ',stack.size()
         print 'top ',stack.peek()
         print 'pop ',stack.pop()
         print 'pop ',stack.pop()
         print 'pop ',stack.pop()
         print 'size ',stack.size()
         print 'top ',stack.peek()
'''tesing result
size  3
top  3
pop  3
pop  2
pop  1
size  0
top 

Traceback (most recent call last):
  File "F:\201408\project\technoloybook\algorithms\codes\Stack.py", line 137, in <module>
    StackNodeMain()
  File "F:\201408\project\technoloybook\algorithms\codes\Stack.py", line 130, in StackNodeMain
    print 'top ',stack.peek()
  File "F:\201408\project\technoloybook\algorithms\codes\Stack.py", line 109, in peek
    raise Exception('StackNode is empty!')
Exception: StackNode is empty!
'''

if __name__ == '__main__':
        
         StackNodeMain()
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值