类练习题2:堆栈和队列类
来源:互联网 发布:驱动保护编程 编辑:程序博客网 时间:2024/04/30 11:15
#!/usr/bin/env python class StackQueue(object): '''Class StackQueue(). The class supports two data structures for both stack and queue. ''' def __init__(self): self.__list = [] def __str__(self): return str(self.__list) __repr__ = __str__ def shift(self): ''' Delete a element from the front of a list. ''' try: print self.__list[0] except IndexError: print "can't delete a element from a empty list." else: self.__list = self.__list[1:] def unshift(self, element): ''' Press a new element into the head of the list. ''' self.__list.insert(0, element) def push(self, element): ''' Add a new element to the end of the list. ''' self.__list.append(element) def pop(self): ''' Return and remove the last element in the list. ''' if hasattr(list, 'pop'): print self.__list.pop() else: try: print self.__list[-1] except IndexError: print "can't delete a element from a empty list." else: self.__list = self.__list[:-1] def _test(): a = StackQueue() print a a.push(2) a.unshift(1) a.push(3) print a a.shift() print a a.pop() print a if __name__ == "__main__": _test()
0 1
- 类练习题2:堆栈和队列类
- 栈和队列练习题
- 字符串堆栈和队列
- 堆栈和队列
- 数据结构-堆栈和队列
- 堆栈和队列
- 【数据结构】堆栈和队列
- js队列和堆栈
- 堆栈和队列
- 数据结构--堆栈和队列
- 队列和堆栈
- 数据结构堆栈和队列
- 数据结构堆栈和队列
- Go队列和堆栈
- PHP 堆栈和队列
- 堆栈和队列
- 数据结构-堆栈和队列
- php 队列、堆栈类V1.1
- ios apple企业账号申请流程
- Activity 学习
- Android 网络通信之 HttpURLConnection
- MobclickAgent
- JavaScript 的性能优化:加载和执行
- 类练习题2:堆栈和队列类
- 定时器的简单示例(setInterval clearInterval)
- MySQL记录存在则更新,不存在插入on duplicate key update
- 使用WebViewJavascriptBridge实现OC与JS交互
- Python刷题笔记(5) - 判断“幸运数”
- R语言基本画图
- make:commands commence before first target
- Android DPI
- session, sessionID和cookie