List vs dual-ended queues

Source: Internet
Author: User

The dual-ended queue supports thread safety, adding and removing at either end of the two ends, with a time complexity of O (1). Access to index access at both ends, with a time complexity of O (1). The time complexity of accessing intermediate elements is O (n). Random access or take a list


List: Time complexity O (n)




Implementation of the double-ended queue:
Class Deque:
# Analog dual-ended queue
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)

List vs dual-ended queues

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.