题解 | #【模板】循环队列#
【模板】循环队列
https://www.nowcoder.com/practice/0a3a216e50004d8bb5da43ad38bcfcbf
思路:给定一个长度为(n+1)的列表,分别以sta_point和end_point作为循环列表的起始指针和终点指针,通过判断两个指针的相对位置来判断是否列表已满或者空。(PS:列表中留有一个不存数字的位置以方便判断)
total_message = input().split(' ') n = int(total_message[0]) q = int(total_message[1]) lis = [None]*(n+1) sta_point = 0 end_point = 0 for i in range(q): opera = input().split(' ') if len(opera) > 1: # push # 先判断是否队列满 if (sta_point - 1 == end_point) or (sta_point == 0 and end_point == n): print('full') else: lis[end_point] = opera[1] end_point = 0 if (end_point == n) else (end_point + 1) continue if sta_point == end_point: print('empty') continue if opera[0] == 'front': print(lis[sta_point]) elif opera[0] == 'pop': print(lis[sta_point]) sta_point = 0 if (sta_point == n) else (sta_point + 1)