Python 使用list实现队列 (基于class, 包含迭代器)

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2015-1-27
@author: beyondzhou
@name: test_listqueue.py
'''

def test_listqueue():

    # import pyListQueue
    from myqueue import pyListQueue

    print '#Init a queue named smith using enqueue'
    smith = pyListQueue()
    smith.enqueue('CSCI-112')
    smith.enqueue('MATH-121')
    smith.enqueue('HIST-340')
    smith.enqueue('ECON-101')

    print '\n#output smith queue'
    for element in smith:
        print element

    print '\n#dequeue one item'
    smith.dequeue()

    print '\n#output smith after dequeue'
    for element in smith:
        print element 

    print '\n#get the length of queue'
    print 'the lenght of queue is ', len(smith)

    print '\n#check wheter the queue is empty'
    if smith.isEmpty():
        print 'queue is empty!'
    else:
        print 'queue is not empty!'

    print '\n#dequeue all items'
    while not smith.isEmpty():
        smith.dequeue()

    print '\n#check wheter the queue is empty after dequeue all items'
    if smith.isEmpty():
        print 'queue is empty!'
    else:
        print 'queue is not empty!'

if __name__ == "__main__":
    test_listqueue()
# Implementation of iter
class _pyListQueueIterator:
    def __init__(self, theList):
        self._setItems = theList
        self._curItem = 0
    def __iter__(self):
        return self
    def next(self):
        if self._curItem < len(self._setItems):
            item = self._setItems[self._curItem]
            self._curItem += 1
            return item
        else:
            raise StopIteration

# Implementation of the Queue ADT using a Python list
class pyListQueue:
    # Creates an empty queue
    def __init__(self):
        self._qList = list()

    # Returns True if the queue is empty
    def isEmpty(self):
        return len(self) == 0

    # Returns the number of items in the queue
    def __len__(self):
        return len(self._qList)

    # Adds the given item to the queue
    def enqueue(self, item):
        self._qList.append(item)

    # Removes and returns the first item in the queue
    def dequeue(self):
        assert not self.isEmpty(), "Cannot dequeue from an empty queue."
        return self._qList.pop(0)

    # Returns an iterator for traversing the list of items
    def __iter__(self):
        return _pyListQueueIterator(self._qList)
#Init a queue named smith using enqueue

#output smith queue
CSCI-112
MATH-121
HIST-340
ECON-101

#dequeue one item

#output smith after dequeue
MATH-121
HIST-340
ECON-101

#get the length of queue
the lenght of queue is  3

#check wheter the queue is empty
queue is not empty!

#dequeue all items

#check wheter the queue is empty after dequeue all items
queue is empty!

时间: 2024-10-04 03:41:11

Python 使用list实现队列 (基于class, 包含迭代器)的相关文章

Python 使用循环数组实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-2 @author: beyondzhou @name: test_arryqueue.py ''' def test_arryqueue(): # import pyListQueue from myqueue import arrayQueue print '#Init a queue named smith using enqueue' smith = array

Python 使用单链表实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-3 @author: beyondzhou @name: test_linklistqueue.py ''' def test_linklistqueue(): # import pyListQueue from myqueue import linkListQueue print '#Init a queue named smith using enqueue' sm

Python 使用list实现堆栈 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-27 @author: beyondzhou @name: test_liststack.py ''' def test_liststack(): # import listStack from mystack import listStack print '#Init a stack named smith using push' smith = listStack(

Python 使用list实现无边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-4 @author: beyondzhou @name: test_listpriorityqueue.py ''' def test_listpriorityqueue(): # import pyListQueue from myqueue import ListPriorityQueue print '#Init a queue named smith using

Python 使用单链表实现堆栈 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-28 @author: beyondzhou @name: test_linkliststack.py ''' def test_linkliststack(): # import linkListStack from mystack import LinkListStack print '#Init a stack named smith using push' sm

Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-6 @author: beyondzhou @name: test_bpriorityqueue.py ''' def test_bpriorityqueue(): # import pyListQueue from myqueue import BPriorityQueue print '#Init a queue named smith using enqueue'

Python与数据结构[2] -&gt; 队列/Queue[0] -&gt; 数组队列的 Python 实现

队列 / Queue 数组队列 数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构. Queue: <--| 1 | 2 | 3 | 4 | 5 |<-- 下面将使用Python中的list来替代C语言中的数组实现数组队列的数据结构. Note: 这里的实现并没有像C语言中的申请一块固定大小的数组,手动的定制数组中队列的头尾位置,而是利用list的特性直接完成,因此较为简单. 数组队列的实现与数组栈的实现基本类似,同时入列和出列也十分简单,仅需要对数组进行操作即

python整合连续数字的练习,包含itertools\groupby用法

#汉字数字转阿拉伯数字 1 class ConvertNum: 2 def __init__(self,cnNum): 3 self.dict = {u'零':0,u'一':1,u'二':2,u'三':3,u'四':4,u'五':5,u'六':6,u'七':7,u'八':8,u'九':9,u'十':10,u'百':100,u'千':1000,u'万':10000} 4 self.cnNum = cnNum 5 6 def convert(self): 7 count = 0 8 result =

Python学习之三大名器-装饰器、迭代器、生成器

Python学习之三大名器-装饰器.迭代器.生成器 一.装饰器     装饰,顾名思义就是在原来的基础上进行美化及完善,器这里指函数,所以说装饰器就是装饰函数,也就是在不改变原来函数的代码及调用方式的前提下对原函数进行功能上的完善.其核心原理其实是利用闭包.     格式 @关键字+装饰函数          被装饰函数()      注意:@行必须顶头写而且是在被装饰函数的正上方     按照形式可以分为:无参装饰器和有参装饰器,有参装饰器即给装饰器加上参数     以下示例是一个无参装饰器,