python数据结构之栈与队列
来源:互联网 发布:网络,监控弱电验收规范 编辑:程序博客网 时间:2024/06/06 19:24
python数据结构之栈与队列
用list实现堆栈stack
堆栈:后进先出
如何进?用append
如何出?用pop()
>>>>>> stack = [3, 4, 5]>>> stack.append(6)>>> stack.append(7)>>> stack[3, 4, 5, 6, 7]>>> stack.pop()7>>> stack[3, 4, 5, 6]>>> stack.pop()6>>> stack.pop()5>>> stack[3, 4]
用list实现队列queue
队列:先进先出
如何进?用append
如何出?用popleft()
>>>>>> from collections import deque>>> queue = deque(["Eric", "John", "Michael"])>>> queue.append("Terry") # Terry arrives>>> queue.append("Graham") # Graham arrives>>> queue.popleft() # The first to arrive now leaves'Eric'>>> queue.popleft() # The second to arrive now leaves'John'>>> queue # Remaining queue in order of arrivaldeque(['Michael', 'Terry', 'Graham'])
自定义堆栈
栈是限定仅在表尾进行插入或删除操作的线性表。对于栈来说,表尾称为栈顶,表头称为栈底。不含元素的空表称为空栈。
堆栈ADT(抽象数据类型)一般提供以下接口:
class Stack(object): """堆栈""" def __init__(self, item = []): self.item = [] if len(item): for i in item: self.item.append(i) def push(self, item): self.item.append(item) def clear(self): del self.item def is_empty(self): return self.size() == 0 def size(self): return len(self.item) def print(self): print(self.item) def top(self): return self.item[-1] def pop(self): data = self.top() self.item.pop() return dataprint("创建堆栈")stack = Stack([1,2,3])stack.print()print("向栈顶插入元素")stack.push(4)stack.print()print("判断堆栈是否为空")print(stack.is_empty())print("返回堆栈中项的个数")print(stack.size())print("返回栈顶的项")print(stack.top())print("删除栈顶的项")stack.pop()stack.print()print("清空堆栈")print(stack.clear())
程序输出:
创建堆栈[1, 2, 3]向栈顶插入元素[1, 2, 3, 4]判断堆栈是否为空False返回堆栈中项的个数4返回栈顶的项4删除栈顶的项[1, 2, 3]清空堆栈None
自定义队列
队列是一种先进先出的线性表。它只允许在表的一端进行插入,在另一端删除元素。
队列ADT(抽象数据类型)一般提供以下接口:
class Queue(object): """模拟队列""" def __init__(self, item = []): self.item = [] if len(item): for i in item: self.item.append(i) def enqueue(self, item): self.item.append(item) def dequeue(self): self.item.pop(0) def clear(self): del self.item def is_empty(self): return self.size() == 0 def size(self): return len(self.item) def print(self): print(self.item)print("创建队列")queue = Queue([1,2,3])queue.print()print("向队列插入元素")queue.enqueue(4)queue.print()print("从队列中删除元素")queue.dequeue()queue.print()print("判断队列是否为空")print(queue.is_empty())print("返回队列中项的个数")print(queue.size())queue.print()print("清空队列")print(queue.clear())
程序输出结果:
创建队列[1, 2, 3]向队列插入元素[1, 2, 3, 4]从队列中删除元素[2, 3, 4]判断队列是否为空False返回队列中项的个数3[2, 3, 4]清空队列None
参考
《数据结构》严蔚敏
https://docs.python.org/3.6/tutorial/datastructures.html#more-on-lists
阅读全文
0 0
- python 数据结构三 之 栈与队列
- python数据结构之栈与队列
- 数据结构之栈与队列
- 数据结构之栈与队列
- 数据结构之:栈与队列
- python数据结构之队列
- 数据结构与算法之----栈与队列
- 数据结构与算法之栈与队列
- 数据结构与算法之栈与队列
- 数据结构与算法之栈与队列
- 数据结构与算法之栈与队列
- python数据结构与算法 16 队列应用之 打印任务
- “栈与队列”之栈--基本数据结构
- python实现数据结构之队列
- 数据结构之栈与队列学习总结
- 数据结构与算法之表、队列、栈
- 数据结构与算法之栈和队列
- 数据结构与算法之栈和队列
- 统计指定目录下的视频时长
- 笔试_jsp试题(2)
- IO库(c++)
- 优化Mysql主从同步延时现象
- String类中常用方法源码解析
- python数据结构之栈与队列
- 总结1
- 找出文件中的兄弟子串(要用hashmap实现)
- HDU 3472 HS BDC 混合图的欧拉路径判断 (网络流)
- Monkey--SDK&APP压力测试参数详解
- CodeWars:Sum of odd numbers
- HTTP协议
- 信息系统集成有以下几个显著特点
- 线程同步