Python implementation of two terminal queue
For an introduction to the two terminal queue, refer to: https://blog.csdn.net/weixin_43790276/article/details/104033337
The data storage structure of the double end queue can be a sequence table or a chain table. In this paper, Python is used to implement the sequence double end queue and the chain double end queue respectively.
1, Implementation of sequential two terminal queue
Sequential double end queue is a double end queue that uses sequence table to store data. All list tuples in Python belong to sequence table. Next, use list to store data to realize sequential double end queue.
# coding=utf-8 class SequenceDoubleQueue(object): def __init__(self): self.__members = list() def is_empty(self): return not len(self.__members) def show(self): if self.is_empty(): print('Double ended queue is empty') return for member in self.__members: if self.__members.index(member) != len(self.__members)-1: print(member, end='|') else: print(member) def head_enter(self, data): self.__members.insert(0, data) def end_enter(self, data): self.__members.append(data) def head_outer(self): if self.is_empty(): return return self.__members.pop(0) def end_outer(self): if self.is_empty(): return return self.__members.pop() def length(self): return len(self.__members) def check(self, index): if index < 0 or index > len(self.__members)-1: raise IndexError return self.__members[index]
Define a SequenceDoubleQueue() class. When instantiating, an empty list will be created to generate an empty sequential double end queue. There are many methods in Python's list, so set the list of stored data to private property to avoid users from chaining other methods of the list outside the class. If the user operates the list directly outside the class, the rule that the dual end queue can only access data from both ends may be broken.
The following are the implementation methods of the sequential dual end queue:
is_empty(): determines whether the sequential double end queue is empty. If the list length of the stored data is zero (corresponding to the Boolean value of False), the sequence double end queue is empty (is "empty" True), otherwise.
show(): display the data in the sequential two terminal queue, that is, print out all the data in the two terminal queue in turn, and traverse the list of stored data to output.
head_enter(data): the front end enters the queue, that is, adding data from the front end of the queue to the queue. In this paper, the beginning of the list is regarded as the front end of the dual end queue, and the end of the list is regarded as the back end of the dual end queue. The front end calls the insert(0, data) method of the list.
End [enter (data): the back end enters the queue, that is, adding data from the back end of the queue to the queue. The backend calls the append(data) method of the list.
head_outer(): the front end exits the queue, that is to say, take out the front-end data from the queue and return the taken data. The pop(0) method of the front-end outgoing call list is enough.
end_outer(): the back-end data is taken out of the queue and returned. The pop() method of the back-end outbound call list is enough.
length(): returns the length of the sequential double ended queue. The length of the sequential two terminal queue is the length of the list of stored data.
check(index): returns the data at the specified location in the sequential two terminal queue. According to the specified index value, return the data corresponding to the index in the list of stored data.
if __name__ == '__main__': sdq = SequenceDoubleQueue() print("is_empty: ", sdq.is_empty()) sdq.show() sdq.head_enter('x') sdq.head_enter('y') sdq.head_enter('z') sdq.end_enter(10) sdq.end_enter(20) sdq.end_enter(30) sdq.show() print(sdq.head_outer()) print(sdq.end_outer()) sdq.show() print("sequence double queue length: ", sdq.length()) print("index member is: ", sdq.check(2))
Operation result:
is_empty: True //Double ended queue is empty z|y|x|10|20|30 z 30 y|x|10|20 sequence double queue length: 4 index member is: 10
2, Implementation of chain double end queue
The chain double end queue is a double end queue that uses the chain list to store data. The chain list is logically ordered and consists of one node, so first declare a class to create a node.
class Node(object): def __init__(self, data): self.data = data self.next = None
Next, we implement the two end queue of the chain in the way of one-way linked list.
class LinkDoubleQueue(object): def __init__(self): self.__head = None def is_empty(self): return not self.__head def show(self): if self.is_empty(): print('Double ended queue is empty') return cur = self.__head while cur is not None: if cur.next is not None: print(cur.data, end='|') else: print(cur.data) cur = cur.next def head_enter(self, data): node = Node(data) node.next = self.__head self.__head = node def end_enter(self, data): if self.is_empty(): self.head_enter(data) return node = Node(data) cur = self.__head while cur.next is not None: cur = cur.next cur.next = node def head_outer(self): if self.is_empty(): return cur = self.__head self.__head = self.__head.next return cur.data def end_outer(self): if self.is_empty(): return cur = self.__head prev = None while cur.next is not None: prev = cur cur = cur.next if cur == self.__head: self.__head = self.__head.next return prev.next = cur.next return cur.data def length(self): length = 0 cur = self.__head while cur is not None: length += 1 cur = cur.next return length def check(self, index): if index < 0 or index >= self.length(): raise IndexError cur = self.__head for _ in range(index): cur = cur.next return cur.data
Define a LinkDoubleQueue() class. When instantiating, an empty chain list will be created to generate an empty chain double end queue.
The following is the implementation of each method of the two end queue of the chain:
is_empty(): judge whether the double end queue of the chain is empty. If the chain header of the stored data points to null (corresponding to the Boolean value of False), the double end queue of the chain is null (is "empty" True), otherwise.
show(): display the data in the double end queue of the chain, that is, print and output all the data in the double end queue in turn, and traverse the chain list storing the data.
head_enter(data): the front end enters the queue, that is, adding data from the front end of the queue to the queue. In this paper, the head of the linked list is regarded as the front end of the double end queue, and the tail of the linked list is regarded as the back end of the double end queue. Front end joining is to add nodes from the head of the list.
End [enter (data): the back end enters the queue, that is, adding data from the back end of the queue to the queue. Back end joining is to add nodes from the end of the list.
head_outer(): the front end exits the queue, that is to say, take out the front-end data from the queue and return the taken data. Front end queuing is to delete and return the data of the chain header node.
end_outer(): the back-end data is taken out of the queue and returned. Back end dequeue is to delete and return the data of the end node of the linked list.
length(): returns the length of the queue at both ends of the chain. The length of the double end queue of the chain is the length of the linked list for storing data.
check(index): returns the data at the specified location in the double end queue of the chain. According to the specified index value, find and return the node data of the corresponding position in the linked list.
ldq = LinkDoubleQueue() print("is_empty: ", ldq.is_empty()) ldq.show() ldq.head_enter('X') ldq.head_enter('Y') ldq.head_enter('Z') ldq.end_enter(100) ldq.end_enter(200) ldq.end_enter(300) ldq.show() print(ldq.head_outer()) print(ldq.end_outer()) ldq.show() print("link queue length: ", ldq.length()) print("index member is: ", ldq.check(2))
Operation result:
is_empty: True //Double ended queue is empty Z|Y|X|100|200|300 Z 300 Y|X|100|200 link queue length: 4 index member is: 100
The above is the sequential double end queue and chain double end queue implemented in Python.