Python Implementation queue using List (class-based, iterator included)

Source: Internet
Author: User

#!/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 the queue ' print ' The lenght of the queue is ', Len (Smith) PR    int ' \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 ' and 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 Iterclass _pylistqueueiterator:def __init__ (self, thelist): Self._setitems = theList Self._curitem = 0 def __iter__ (self): return self def next (self): if Self._curitem < Len (self._set            Items): item = Self._setitems[self._curitem] Self._curitem + = 1 return item Else:  Raise Stopiteration # Implementation of the Queue ADT using a Python listclass pylistqueue: # creates An empty queue def __init__ (self): Self._qlist = List () # Returns True If the queue is empty def isEmpty ( Self): return len = = 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) # Remove S 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 _pyl Istqueueiterator (Self._qlist)

#Init a queue named Smith using Enqueue#output Smith queuecsci-112math-121hist-340econ-101#dequeue one item#output Smith a fter dequeuemath-121hist-340econ-101#get The length of queuethe lenght of the queue is  3#check wheter the queue is Emptyqu Eue isn't empty! #dequeue all Items#check wheter the queue was empty after dequeue all Itemsqueue is empty!

Python Implementation queue using List (class-based, iterator included)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.