Python implementation of Python and data structure [2], queue/queue[0], array queue

Source: Internet
Author: User
Tags iterable

Queue /Queue

Array queue

Array queue is an array-based implementation of the queue, its implementation is similar to the array stack, is a FIFO linear data structure.

Queue:        <--| 1 | 2 | 3 | 4 | 5 |<--

The following will use the list in Python instead of the array in the C language to implement the data structure of the array queue.

Note: The implementation here does not apply a fixed-size array like the one in C, manually customizing the queue's kinsoku position in the array, but using the list's features to do it directly, so it is simpler.

The implementation of the array queue is similar to the implementation of the array stack, and the into row and the dequeue are very simple, only need to operate the arrays.

The complete code and running results are given here, and the implementation process can refer to the implementation of the array stack.

Subsequent re-replenishment of the fixed array size of the queue implementation.

Full code

1 classQueueemptyexception (Exception):Pass2 3 4 classQueuefullexception (Exception):Pass5 6 7 classQueue:8     """9 Queue:Ten <--| 1 | 2 | 3 | 4 | 5 |<-- One     """ A     def __init__(Self, max=0): -Self.queue = [] -Self._max =Max theSelf.max =Max -  -     def __str__(self): -         return ' | '. Join (Map (str, self.queue)) +  -     defInit (self, iterable=()): +         if  notiterable: A             return  at self.queue.extend (List (iterable)) -  - @property -     defMax (self): -         returnSelf._max -  in @max. Setter -     defMax (self, m): tom =Int (m) +         ifM <self.length: -             RaiseException ('Resize queue failed, please dequeue some elements first.') theSelf._max =m *         ifSelf._max <0: $Self._max =0Panax Notoginseng  -     defShow (self): the         Print(self) +  A @property the     defLength (self): +         returnLen (self.queue) -  $ @property $     defIs_empty (self): -         return  notbool (self.queue) -  the @property -     defIs_full (self):Wuyi         returnBOOL (Self._max andSelf.length = =Self._max) the  -     defEnqueue (self, item): Wu         ifSelf.is_full: -             RaiseQueuefullexception ('error:trying to enqueue element into a full queue') About self.queue.append (item) $      -     defdequeue (self): -         ifSelf.is_empty: -             RaiseQueueemptyexception ('error:trying to dequeue element from an empty queue') AFront =Self.queue[0] +Self.queue = self.queue[1:] the         returnFront -  $     defClear (self): theSelf.queue = [] the  the  the defTest (queue): -     Print('\ninit Queue:') inQueue.init ([1, 2, 3, 4, 5, 6, 7]) the queue.show () the  About     Print('\nenqueue element to queue:') theQueue.enqueue (' like') the queue.show () the  +     Print('\ndequeue element from the queue:') -E =Queue.dequeue () the     Print('Element%s deququed,'%e)Bayi queue.show () the  the     Print('\nset queue max size:') -     Try: -Queue.max = 1 the     exceptException as E: the         Print(e) the  the     Print('\nset queue max size:') -Queue.max = 7 the     Print(Queue.max) the  the     Print('\nenqueue Full queue:')94     Try: theQueue.enqueue (7) the     exceptqueuefullexception as E: the         Print(e)98  About     Print('\nclear Queue:') - queue.clear ()101 queue.show ()102 103     Print('\nqueue is empty:')104     Print(Queue.is_empty) the 106     Print('\ndequeue Empty queue:')107     Try:108 Queue.dequeue ()109     exceptqueueemptyexception as E: the         Print(e)111  the if __name__=='__main__':113Test (Queue ())

Run to get results

Init queue:1 | 2 | 3 | 4 | 5 | 6 | 7Enqueue element to queue:1 | 2 | 3 | 4 | 5 | 6 | 7 |  from1 deququed,2 | 3 | 4 | 5 | 6 | 7 | likeset queue Max size:resize queue failed, please dequeue some elements first. Set Queue max Size:7 is from an empty queue

Related reading

1 array Stack

Python implementation of Python and data structure [2], queue/queue[0], array queue

Related Article

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.