+ """double-linked list implementation with sentinel"""
+ """create an empty list in O(1)"""
+ self.__sentinel = node()
+ self.__sentinel._next = self.__sentinel
+ self.__sentinel._prev = self.__sentinel
+ """return the length of the list in O(1)"""
+ def __contains__(self,x):
+ """return if x is in the list in O(N)"""
+ return self.__contains_aux(x,self.__sentinel._next)
+ def __contains_aux(self,x,nd):
+ """auxiliary search function in O(N)"""
+ if nd == self.__sentinel:
+ ans = self.__contains_aux(x,nd._next)
+ """return the string representation of the list in O(N)"""
+ self.__str_aux(ans,self.__sentinel._next)
+ def __str_aux(self,ans,nd):
+ if nd != self.__sentinel:
+ self.__str_aux(ans,nd._next)
+ def insert_last(self,x):
+ """insert an element to the end of the list in O(1)"""
+ prev = self.__sentinel._prev
+ nd = node(prev,x,self.__sentinel)
+ prev._next = self.__sentinel._prev = nd
+ def insert_first(self,x):
+ """insert an element to the head of the list in O(1)"""
+ nxt = self.__sentinel._next
+ nd = node(self.__sentinel,x,nxt)
+ nxt._prev = self.__sentinel._next = nd
+ """delete the element last in the list, if any in O(1)"""
+ assert not(self.is_empty())
+ last = self.__sentinel._prev
+ prev = self.__sentinel._prev = last._prev
+ prev._next = self.__sentinel
+ last._next = last._prev = None
+ def remove_first(self):
+ """delete the element first in the list, if any in O(1)"""
+ assert not(self.is_empty())
+ first = self.__sentinel._next
+ nxt = self.__sentinel._next = first._next
+ nxt._prev = self.__sentinel
+ first._next = first._prev = None
+ """return if the list is empty in O(1)"""
+ """return the element last in the list, if any, in O(1)"""
+ assert not(self.is_empty())
+ return self.__sentinel._prev._val
+ """return the element first in the list, if any, in O(1)"""
+ assert not(self.is_empty())
+ return self.__sentinel._next._val
+ """node implementation with previous, value, and next fields"""
+ def __init__(self,prev=None,val=None,nxt=None):
+ """creates an empty node in O(1)"""
+ """return the string representation of the node in O(1)"""