免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1209 | 回复: 0
打印 上一主题 下一主题

菜鸟自己写的练习 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-14 12:16 |只看该作者 |倒序浏览
# -*- coding: cp936 -*-

"""

class Queue(object):

    def __init__(self,listappend):

        self.list=listappend

    def enqueue(self,list1):

        self.list1=list1

        self.list.append(self.list1)

        print'增加新元素为:',self.list1

        print'更新为:',self.list

    def dequeue(self):

        del1=self.list[0]

        self.list2=self.list[1:]

        print'删除元素为:',del1

        print'删除后的元素更新为:',self.list2




a=Queue([1,2,3,4,5])

b=raw_input('输入元素为:')

a.enqueue(b)

a.dequeue()

"""

class StQu(object):

    def __init__(self,list1):

        self.list1=list1

    """

    def unshift(self, headItem):

        tempList = [headItem]

        for item in self.list1:

            tempList.append(item)

        self.list1 = tempList

        print 'Item ', headItem, ' is added on the head of array'

        print 'The updated array is: ',self.list1, '\n'

    """

    def unshift(self,insert1):#插入到第一个元素

        self.insert1=insert1

        #self.list1.insert(self.insert1)

        #print'插入后的元素为:',self.list2

        #print'当前:',self.list1

        templist=[self.insert1]

        for temp1 in self.list1:

            templist.append(temp1)

        self.list1=templist

        print'增加头部:',templist

        print'当前列表元素:',self.list1

    def push(self,l):

        self.l=l

        self.list1.append(self.l)

        print'在尾部增加新元素为:',self.l

        print'更新尾部为:',self.list1

    def shift(self):

        del1=self.list1[0]

        self.del2=self.list1[1:]

        print'删除列表头部元素为:',del1

        print'删除后列表头部更新为:',self.del2

    def pop(self):

        pop1=self.list1.pop()

        print'删除最后一个元素:',pop1

        print'删除最后一个元素更新:',self.list1

        

a=StQu([1,2,3,4,5,6,7])

a.unshift(100)

a.shift()

a.push(10)

a.pop()












   








您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP