def test(self): l1 = LinkedList() l1.append(57) l1.append("hello") l1.append(None) print l1 print "isEmpty returns %s" % (l1.isempty) ptr = l1.head while ptr is not l1.tail: print ptr.data ptr = ptr.next l1.extract(57) ptr = l1.head while ptr is not l1.tail: print ptr.data ptr = ptr.next l2 = copy(l1) print l2 print l2.length print l2.headdata print l2.taildata
class StackAsLinkedList(Stack): def __init__(self): super(StackAsLinkedList, self).__init__() self._list = LinkedList() self._count = 0 def purge(self): self._list.purge() self._count = 0 def push(self, obj): self._list.prepend(obj) self._count += 1 def pop(self): if(self._count == 0): raise ContainerEmpty result = self._list.headdata self._list.extract(result) self._count -= 1 return result def gettop(self): if self._count == 0: raise ContainerEmpty return self._list.headdata def accept(self, visitor): assert isinstance(visitor, Visitor) pre = self._list.head while pre is not None: visitor.visit(pre.data) if visitor.isdone: return pre = pre.next class Iterator(Iterator): def __init__(self, stack): super(StackAsLinkedList.Iterator, self).__init__(stack) self._position = stack._list.head def next(self): if self._position is None: raise StopIteration element = self._position self._position = self._position.next return element.data def __iter__(self): return self.Iterator(self) def _compareto(self, obj): assert isinstance(self, obj.__class__) raise MethodNotImplemented
class QueueAsLinkedList(Queue): def __init__(self): super(QueueAsLinkedList, self).__init__() self._list = LinkedList() def purge(self): self._list.purge() self._count = 0 def gethead(self): if self._count == 0: raise ContainerEmpty return self._list.headdata head = property(fget = lambda self: self.gethead()) def enqueue(self, data): self._list.append(data) self._count = self._count + 1 def dequeue(self): if self._count == 0: raise ContainerEmpty result = self._list.headdata self._list.extract(result) self._count = self._count - 1 return result def accept(self, visitor): assert isinstance(visitor, Visitor) pre = self._list.head while pre is not None: visitor.visit(pre.data) if visitor.isdone: return pre = pre.next class Iterator(Iterator): def __init__(self, queue): super(QueueAsLinkedList.Iterator, self).__init__(queue) self._position = queue._list.head def next(self): if self._position is None: raise StopIteration element = self._position self._position = self._position.next return element.data def __iter__(self): return self.Iterator(self) def _compareto(self, obj): assert isinstance(self, obj.__class__) raise MethodNotImplemented
def __init__(self): super(QueueAsLinkedList, self).__init__() self._list = LinkedList()
def __init__(self): super(StackAsLinkedList, self).__init__() self._list = LinkedList() self._count = 0