Python data structures and algorithms - queue

queue

1. Definition: linear limiting queue table insert and delete operations at both ends, allowing one ends into operation referred to as "the tail", delete operation is allowed is referred to as "head-of."

2. Features:

  • Data manipulation in the queue can queue head and queue tail
  • Law FIFO stack model or the rear of the last-named

3. The implementation of a queue Code

There operation queue enqueue, dequeue, full-empty queue operations such determination.

. 1  "" " 
2  squeue.py the queues of storing
 three  key codes
 . 4  
. 5  ideas Analysis:
 6  1. Based on the complete list storage structure
 7  2. Package predetermined operation queue head and queue tail
 8  '' ' 
. 9  
10  #   custom queue exception 
. 11  class QueueError (exception):
 12 is    Pass 
13 is  
14  
15  # queue operation class 
16  class squeue:
 . 17    DEF  the __init__ (Self):
 18 is      self._elems = []
 . 19  
20 is    # Analyzing empty queue 
21 is    DEF is_empty (Self):
 22 is     return self._elems == []
 23 is  
24    # enqueue end of the list 
25    DEF the enqueue (Self, elem):
 26 is      self._elems.append (elem)
 27  
28    # dequeued from the head of the list 
29    DEF dequeue (Self):
 30      IF  Not self._elems:
 31 is        The raise QueueError ( " Queue empty IS " )
 32      return self._elems.pop (0)
 33 is  
34 is  
35  IF  the __name__ == " __main__ " :
 36    sq. = SQueue()
37   sq.enqueue(10)
38   sq.enqueue(20)
39   sq.enqueue(30)
40   while not sq.is_empty():
41     print(sq.dequeue())
The code sequence stored in the queue
. 1  "" " 
2  lqueue.py queue chain
 3  key codes
 . 4  
. 5  ideas Analysis:
 6  1. completed chain model based on the stack list
 7  2 as the beginning of the linked list queue head, tail as the tail
 8  3. The head and tail point to the same a node as an empty queue
 9  "" " 
10  
. 11  #   custom queue exception 
12 is  class QueueError (exception):
 13 is    Pass 
14  
15  # of the node class 
16  class the node:
 . 17    DEF  the __init__ (self, Data, Next = None):
 18 is      self = .data Data
 . 19      self.next = Next
 20 is 
21  # chain class queue 
22 is  class LQueue:
 23 is    DEF  the __init__ (Self):
 24      # initial head and tail point to a moot node 
25      self.front = = self.rear the Node (None)
 26 is  
27    DEF is_empty (Self):
 28      return self.front == self.rear
 29  
30    #  into the tail actuator 
31 is    DEF the enqueue (Self, elem):
 32      self.rear.next = the Node (elem)
 33 is      self.rear = self.rear.next
 34 is  
35    #  a movable head 
36    DEF dequeue(self):
37     if self.front == self.rear:
38       raise QueueError("Queue is empty")
39     self.front = self.front.next
40     return self.front.data
41 
42 if __name__ == "__main__":
43   lq = LQueue()
44   lq.enqueue(10)
45   lq.enqueue(20)
46   lq.enqueue(30)
47   print(lq.dequeue())
Queue chain stores code

Guess you like

Origin www.cnblogs.com/maplethefox/p/10988580.html