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 = arrayQueue(4)
    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 

    smith.enqueue('ECON-102')
    print '\n#output smith after enqueue again'
    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!'

    print '\n#init again'
    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

if __name__ == "__main__":
    test_arryqueue()
# Implementation of the Queue ADT using a circular array
class _ArrayQueueIterator:
    def __init__(self, theArray, front, count):
        self._arrayRef = theArray
        self._curNdx = front
        self._count = count
        self._index = 0

    def __iter__(self):
        return self

    def next(self):
        if self._index < self._count:
            entry = self._arrayRef[self._curNdx]
            self._curNdx = (self._curNdx+1) % len(self._arrayRef)
            self._index = self._index + 1
            return entry
        else:
            raise StopIteration

from myarray import Array

class arrayQueue:
    # Creates an empty queue
    def __init__(self, maxSize):
        self._count = 0
        self._front = 0
        self._back = maxSize - 1
        self._qArray = Array(maxSize)

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

    # Returns True if the queue is full
    def isFull(self):
        return self._count == len(self._qArray)

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

    # Adds the given item to the queue
    def enqueue(self, item):
        assert not self.isFull(), "Cannot enqueue to a full queue."
        maxSize = len(self._qArray)
        self._back = (self._back + 1) % maxSize
        self._qArray[self._back] = item
        self._count += 1

    # Removes and returns the first item in the queue
    def dequeue(self):
        assert not self.isEmpty(), "Cannot dequeue from an empty queue."
        item = self._qArray[self._front]
        maxSize = len(self._qArray)
        self._front = (self._front + 1) % maxSize
        self._count -= 1
        return item

    # Returns the array queue's iterator for traversing the elements
    def __iter__(self):
        return _ArrayQueueIterator(self._qArray, self._front, self._count)
#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

#output smith after enqueue again
MATH-121
HIST-340
ECON-101
ECON-102

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

#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!

#init again

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

时间: 2024-11-05 18:37:37

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

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_listqueue.py ''' def test_listqueue(): # import pyListQueue from myqueue import pyListQueue print '#Init a queue named smith using enqueue' smith = pyL

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 使用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 使用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(

基于循环数组的无锁队列

在之前的两篇博客(线程安全的无锁RingBuffer的实现,多个写线程一个读线程的无锁队列实现)中,分别写了在只有一个读线程.一个写线程的情况下,以及只有一个写线程.两个读线程的情况下,不采用加锁技术,甚至原子运算的循环队列的实现.但是,在其他的情况下,我们也需要尽可能高效的线程安全的队列的实现.本文实现了一种基于循环数组和原子运算的无锁队列.采用原子运算(compare and swap)而不是加锁同步,可以很大的提高运行效率.之所以用循环数组,是因为这样在使用过程中不需要反复开辟内存空间,可

【队列】基于循环数组的实现

Description 请完成以下队列类的实现:(请注意数组实现应该为循环数组) enum ErrorCode { success, underflow, overflow }; const int maxQueue = 100; template <class QueueEntry> class MyQueue { public: MyQueue(); bool empty() const;  // 判断队列是否为空 ErrorCode append(const QueueEntry &am

JAVA实现数组队列,循环数组队列,链式队列

/** * 文件名:QueueText.java * 时间:2014年10月22日下午9:05:13 * 作者:修维康 */ package chapter3; /** * 类名:ArrayQueue * 说明:队列的数组实现 */ class ArrayQueue<AnyType>{ private static final int DEFAULT_CAPACITY = 10; private int front;//队头 private int rear;//队尾 private int