Python uses a single-linked list to implement queues (class-based, including iterators)

Source: Internet
Author: User

#!/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 S Mith using Enqueue ' Smith = Linklistqueue () 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 Elemen T print ' \n#dequeue one item ' Smith.dequeue () print ' \n#output Smith after dequeue ' for element i n smith:print element Smith.enqueue (' ECON-102 ') print ' \n#output Smith after enqueue again ' fo R element in Smith:print element print ' \n#get the length of the queue ' print ' The lenght of the 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 ' 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! ' print ' \n#init again ' smith.enqueue (' CSCI-112 ') smith.enqueue (' MATH-121 ') smith.enqueue (' HIST-340 ') smith.enqu Eue (' ECON-101 ') print ' \n#output Smith queue ' for element in Smith:print element if __name__ = = "__ma In__ ": Test_linklistqueue ()

# Implementation of the Queue ADT using a linked ListClass Linklistqueue: # Creates an empty Queue def __init__ (self ): Self._qhead = None Self._qtail = None Self._count = 0 # Returns True if the queue is empty D        EF IsEmpty (self): return self._qhead was None # 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): node = _linklistqueuenode (item) if Self.isempty (): self._qhead = node Else:self._qtail.next = node Self . _qtail = node Self._count + = 1 # removes and returns the first item in the Queue Def dequeue (self): a        Ssert not Self.isempty (), "cannot dequeue from an empty queue." node = Self._qhead if Self._qhead is self._qtail:self._qtail = None Self._qhead = self._qhead.ne XT Self._count-= 1 return Node.item # Returns an iterator For traversing the list of items Def __iter__ (self): return _linklistqueueiterator (self._qhead) # Private Storag E class for creating the linked list Nodesclass _linklistqueuenode (object): Def __init__ (self, item): Self.item = Item Self.next = none# defines a linked list iterator class _linklistqueueiterator:def __init__ (self, queuehe  AD): Self._curnode = Queuehead def __iter__ (self): return self def next (self): if Self._curnode is none:raise stopiteration Else:item = Self._curnode.item Self._curnode = self . _curnode.next Return Item

#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#output Smith after enqueue againmath-121hist-340econ-101econ-102#get the length  of queuethe lenght of the queue is  4#check wheter the queue is Emptyqueue are not empty! #dequeue all Items#check wheter the Queue is empty after dequeue all itemsqueue are empty! #init again#output Smith queuecsci-112math-121hist-340econ-101

Python uses a single-linked list to implement queues (class-based, including iterators)

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.