题解 | #【模板】栈#

【模板】栈

http://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

stack_list = []

判断是否为空

def IsEmpty(num_list): if num_list == []: return True else: return False result_list =[] num_count = int(input()) while num_count: caozuo_list = input().split(' ') # print(caozuo_list) if len(caozuo_list) == 2: caozuo_name = caozuo_list[0] caozuo_num = int(caozuo_list[1]) stack_list.append(caozuo_num) else: caozuo_name = caozuo_list[0] if caozuo_name == 'pop': if IsEmpty(stack_list) == True: result_list.append('error') else: result_list.append(stack_list[len(stack_list)-1]) del stack_list[len(stack_list)-1] elif caozuo_name == 'top': if IsEmpty(stack_list) == True: result_list.append('error') else: result_list.append(stack_list[len(stack_list)-1]) num_count -=1

for i in result_list: print(i)

全部评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
2 收藏 评论
分享
牛客网
牛客企业服务