Python第六天课后作业
2017-09-04 16:48
495 查看
第一题:以面向对象的方式,实现队列和栈的数据结构。
部分运行截屏展示
![](https://img-blog.csdn.net/20170904164522979?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2l5dWV4aWFuZ3hpYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20170904164647416?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2l5dWV4aWFuZ3hpYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
stack=[] queue=[] class Stack(): def Push_Stack(self): #入栈,向列表尾部添加元素 item=raw_input("入栈元素:") stack.append(item) def Pop_Stack(self): #出栈 if len(stack)==0: print "空栈" stack.pop() def Info_Stack(self): #打印栈元素 for i in stack: print i, def getSize(self): #打印栈长度 print len(stack) class Queue(): def En_Queue(self): #入队列,向列表头部插入元素 item = raw_input("入队列元素:") queue.insert(0, item) def De_Queue(self): #出队列 if len(queue) == 0: print "空队列" queue.pop() def Info_Queue(self): #打印队列元素 for i in queue: print i, def getSize(self): #打印队列长度 print len(queue) def Stack_Menu(): while True: Stack_Menu = """ 请选择: 1. 入栈 2. 出栈 3. 显示栈元素 4. 显示栈长度 5. 返回主菜单 """ choice = input(Stack_Menu) if choice==1: s.Push_Stack() elif choice==2: s.Pop_Stack() elif choice==3: s.Info_Stack() elif choice==4: s.getSize() elif choice==5: return 1 def Queue_Menu(): while True: Queue_Menu = """ 请选择: 1. 入队列 2. 出队列 3. 显示队列元素 4. 显示队列长度 5. 返回主菜单 """ choice = input(Queue_Menu) if choice==1: q.En_Queue() elif choice==2: q.De_Queue() elif choice==3: q.Info_Queue() elif choice==4: q.getSize() elif choice==5: return 1 s=Stack() q=Queue() while True: Menu = """ ****** 欢迎来到栈与队列的管理系统!****** 1.栈的管理 2.队列的管理 3.退出 ************************************* 请选择:""" choice=input(Menu) if choice==1: Stack_Menu() elif choice==2: Queue_Menu() elif choice==3: exit(0) else: print "\n输入错误。请输入序号(1-2)"
部分运行截屏展示
相关文章推荐
- Python第六天课后作业
- Python第六天课后作业
- Python第六天课后作业
- Python第六天课后作业
- 【我们一起自学Python】-课后作业:编写登陆接口
- Python第一天课后作业
- Python3-小甲鱼教程 【课后作业及答案】004改进我们的小游戏
- Python第七章课后作业
- Python第三章课后作业
- Python第一天课后作业
- Python第一天课后作业
- Python第四章课后作业
- Python第一天课后作业
- Python第一天课后作业
- Python第八章课后作业(函数)
- Python第五章课后作业
- DC学院课程-Python爬虫(入门+进阶)-课后作业分析
- python课后作业1
- Python第六章课后作业
- Python第四天课后作业