Deque implemented abstract data types and python

Deque implemented abstract data types and python

Deque Deque there is a number of data sets
similar with the queue, which ends may be referred to as "first", "tail" ends, but deque data item may be added to the first team, the team may be added from the end;
data items may also be removed from both ends.
In a sense, double-ended queue integrated stack and the ability to queue.

However deque have intrinsic characteristics LIFO or FIFO

If deque stack or queue to simulate the
needs of the user's own operation to maintain consistency

Deque Abstract Data Types

Deque() :  创建一个空双端队列
addFront(item):  将item加入队首
addRear(item):  将item加入队尾
removeFront():  从队首移除数据项,返回值为移除的数据项
removeRear():  从队尾移除数据项,返回值为移除数据项
isEmpty():  返回deque是否为空
size() :   返回值deque中包含数据项的个数

List adopted to achieve

List index 0 as the trailing end of the deque
List -1 as the index of the head end deque

Operational complexity

addFront/removeFront O(1)

addRear/removeRear O(n)

class Deque:
	def __init__(self):
		self.items = []
	def isEmpty(self):
		return self.items == []
	def addFront(self, item):
		self.items.append(item)
	def addRear(self, item):
		self.items.insert(0, item)
	def removeFront(self):
		return self.items.pop()
	def removeRear(self):
		return self.items.pop(0)
	def size(self):
		return len(self.items)
Published 25 original articles · won praise 3 · Views 489

Guess you like

Origin blog.csdn.net/qq_44045101/article/details/103263555