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 = 0if x:for i in x:self.add(i)- self.length = 0
- self.addAll(x)
def __str__(self):if self.start != None:current = self.startout = '[ ' + str(current.data)while current.next != None:current = current.nextout += ' '+ 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.startfor 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.nextcurrent.next.prev = Noneself.lenght -= 1if current.next == None:if current.prev != None:current.prev.next = Noneself.last = current.prevself.last.next = Noneself.lenght -= 1if current.prev != None and current.next != None:current.prev.next = current.nextcurrent.next.prev = current.prevself.lenght -= 1if current.next == None and current.prev == None:clear(self)self.lenght -= 0if remove_all == False:- self.remove_node(current)
- if not remove_all:
- break
- current = current.next
# TODO: Replace examples and use TDD development by writing your own tests # These are some of the methods available: # test.expect(boolean, [optional] message) # test.assert_equals(actual, expected, [optional] message) # test.assert_not_equals(actual, expected, [optional] message) # You can use Test.describe and Test.it to write BDD style test groupings test.assert_equals(LinkedList(1,2,3,4,5).length, 5) test.assert_equals(LinkedList().length, 0) l = LinkedList(1,2,3,4,5) test.assert_equals(str(l), '[ 1 2 3 4 5 ]') l.remove(4) test.assert_equals(str(l), '[ 1 2 3 5 ]')
- # TODO: Replace examples and use TDD development by writing your own tests
- # These are some of the methods available:
- # test.expect(boolean, [optional] message)
- # test.assert_equals(actual, expected, [optional] message)
- # test.assert_not_equals(actual, expected, [optional] message)
- # You can use Test.describe and Test.it to write BDD style test groupings
test.assert_equals(LinkedList(1,2,3,4,5).lenght, 5)- test.assert_equals(LinkedList(1,2,3,4,5).length, 5)
- test.assert_equals(LinkedList().length, 0)
- l = LinkedList(1,2,3,4,5)
- test.assert_equals(str(l), '[ 1 2 3 4 5 ]')
- l.remove(4)
- test.assert_equals(str(l), '[ 1 2 3 5 ]')