Ad
Code
Diff
  • class Node:
        def __init__(self, x, Next, Prev):
            self.data = x
            self.next = Next
            self.prev = Prev
          
        
    class LinkedListIterator:
        def __init__(self, node):
            self.node = node
        
        def __iter__(self):
            return self
                
        def __next__(self):
            if not self.node:
                raise StopIteration
            value = self.node.data
            self.node = self.node.next        
            return value
    
           
    class LinkedList:
        def __init__(self, *x):
            self.start = None
            self.last = None
            self.length = 0
            self.addAll(x)
            
        def __iter__(self):
            return LinkedListIterator(self.start)
            
        def __str__(self):        
            out = '[ '+' '.join(map(str, self))+' ]'
            return out
        
        def clear(self):
            self.start = None
            self.last = None
            self.length = 0
            
        def add(self, x):
            if self.start == None:
                self.start = self.last = Node(x,None,None)
                self.length = 1
            else:
                prev = self.last
                self.last.next = self.last = Node(x,None,None)
                self.last.prev = prev
                self.length += 1
                
        def addAll(self, xs):        
            for i in xs:
                self.add(i)
        
        
        def remove_node(self, node):
            if not node.prev and node.next:            
                self.start = node.next
                node.next.prev = None
                self.length -= 1
                    
            elif not node.next and node.prev:            
                node.prev.next = None
                self.last = node.prev
                self.last.next = None
                self.length -= 1
                
            elif node.prev and node.next:
                node.prev.next = node.next
                node.next.prev = node.prev
                self.length -= 1
            else:
                self.clear()
                                
                
        def remove(self, x, remove_all = False):
            current = self.start        
            while current:
                if current.data == x:
                    self.remove_node(current)
                    if not remove_all:
                        break
                current = current.next
    • class Node:
    • def __init__(self,x,Next, Prev):
    • def __init__(self, x, Next, Prev):
    • self.data = x
    • self.next = Next
    • self.prev = Prev
    • class LinkedListIterator:
    • def __init__(self, node):
    • self.node = node
    • def __iter__(self):
    • return self
    • def __next__(self):
    • if not self.node:
    • raise StopIteration
    • value = self.node.data
    • self.node = self.node.next
    • return value
    • class LinkedList:
    • def __init__(self, *x):
    • self.start = None
    • self.last = None
    • self.lenght = 0
    • if x:
    • for i in x:
    • self.add(i)
    • self.length = 0
    • self.addAll(x)
    • def __str__(self):
    • if self.start != None:
    • current = self.start
    • out = '[ ' + str(current.data)
    • while current.next != None:
    • current = current.next
    • out += ' '+ str(current.data)
    • out += ' ]'
    • return out
    • def __iter__(self):
    • return LinkedListIterator(self.start)
    • def __str__(self):
    • out = '[ '+' '.join(map(str, self))+' ]'
    • return out
    • def clear(self):
    • self.__init__()
    • self.start = None
    • self.last = None
    • self.length = 0
    • def add(self, x):
    • if self.start == None:
    • self.start = self.last = Node(x,None,None)
    • self.lenght += 1
    • self.length = 1
    • else:
    • prev = self.last
    • self.last.next = self.last = Node(x,None,None)
    • self.last.prev = prev
    • self.lenght += 1
    • self.length += 1
    • def addFew(self,*x):
    • if x:
    • for i in x:
    • self.add(i)
    • def addAll(self, xs):
    • for i in xs:
    • self.add(i)
    • def remove_node(self, node):
    • if not node.prev and node.next:
    • self.start = node.next
    • node.next.prev = None
    • self.length -= 1
    • elif not node.next and node.prev:
    • node.prev.next = None
    • self.last = node.prev
    • self.last.next = None
    • self.length -= 1
    • elif node.prev and node.next:
    • node.prev.next = node.next
    • node.next.prev = node.prev
    • self.length -= 1
    • else:
    • self.clear()
    • def remove(self, x,remove_all = False):
    • current = self.start
    • for i in range(self.lenght):
    • def remove(self, x, remove_all = False):
    • current = self.start
    • while current:
    • if current.data == x:
    • if current.prev == None:
    • if current.next != None:
    • self.start = current.next
    • current.next.prev = None
    • self.lenght -= 1
    • if current.next == None:
    • if current.prev != None:
    • current.prev.next = None
    • self.last = current.prev
    • self.last.next = None
    • self.lenght -= 1
    • if current.prev != None and current.next != None:
    • current.prev.next = current.next
    • current.next.prev = current.prev
    • self.lenght -= 1
    • if current.next == None and current.prev == None:
    • clear(self)
    • self.lenght -= 0
    • if remove_all == False:
    • self.remove_node(current)
    • if not remove_all:
    • break
    • current = current.next