Stack and queue.

本文详细介绍了队列这一数据结构的定义与基本运算,包括队列的插入与删除操作、判空与判满等逻辑运算,以及队列的先进先出(FIFO)特性。

队列的定义及基本运算

1、定义
     队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表

        

  (1)允许删除的一端称为队头(Front)
  (2)允许插入的一端称为队尾(Rear)
  (3)当队列中没有元素时称为空队列
  (4)队列亦称作先进先出(First In First Out)的线性表,简称为FIFO表
     队列的修改是依先进先出的原则进行的。新来的成员总是加入队尾(即不允许"加塞"),每次离开的成员总是队列头上的(不允许中途离队),即当前"最老的"成员离队。
 【例】在队列中依次加入元素a1,a2,…,an之后,a1是队头元素,an是队尾元素。退出队列的次序只能是a1,a2,…,an

2、队列的基本逻辑运算
(1)InitQueue(Q)
     置空队。构造一个空队列Q。

(2)QueueEmpty(Q)
     判队空。若队列Q为空,则返回真值,否则返回假值。

(3) QueueFull(Q)

     判队满。若队列Q为满,则返回真值,否则返回假值。
  注意:
     此操作只适用于队列的顺序存储结构。

(4) EnQueue(Q,x)

     若队列Q非满,则将元素x插入Q的队尾。此操作简称入队

(5) DeQueue(Q)

     若队列Q非空,则删去Q的队头元素,并返回该元素。此操作简称出队

(6) QueueFront(Q)

     若队列Q非空,则返回队头元素,但不改变队列Q的状态。

 

详细说明可以参考链接:

http://student.zjzk.cn/course_ware/data_structure/web/zhanhuoduilie/zhanhuoduilie3.2.1.htm

转载于:https://www.cnblogs.com/AlwaysOnLines/p/3979263.html

def apply_operator(operator, operand1, operand2=None): if operator == 'and': return operand1 and operand2 elif operator == 'or': return operand1 or operand2 elif operator == 'not': return not operand1 def evaluate_expression(expression): precedence = {'not': 3, 'and': 2, 'or': 1} output_queue = [] operator_stack = [] tokens = expression.replace('(', ' ( ').replace(')', ' ) ').split() for token in tokens: if token == 'true': output_queue.append(True) elif token == 'false': output_queue.append(False) elif token in ('and', 'or', 'not'): while (operator_stack and operator_stack[-1] != '(' and precedence.get(operator_stack[-1], 0) >= precedence[token]): op = operator_stack.pop() if op == 'not': operand = output_queue.pop() output_queue.append(apply_operator(op, operand)) else: operand2 = output_queue.pop() operand1 = output_queue.pop() output_queue.append(apply_operator(op, operand1, operand2)) operator_stack.append(token) elif token == '(': operator_stack.append(token) elif token == ')': while operator_stack and operator_stack[-1] != '(': op = operator_stack.pop() if op == 'not': operand = output_queue.pop() output_queue.append(apply_operator(op, operand)) else: operand2 = output_queue.pop() operand1 = output_queue.pop() output_queue.append(apply_operator(op, operand1, operand2)) operator_stack.pop() # Pop the '(' while operator_stack: op = operator_stack.pop() if op == 'not': operand = output_queue.pop() output_queue.append(apply_operator(op, operand)) else: operand2 = output_queue.pop() operand1 = output_queue.pop() output_queue.append(apply_operator(op, operand1, operand2)) result = output_queue.pop() return 'true' if result else 'false' # 测试示例 expression = "not (true and (true or not false))" print(evaluate_expression(expression))
最新发布
11-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值