Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
class List:
|
||||
def __init__(self, data, next=None, prev=None):
|
||||
self.data = data
|
||||
self.next = next
|
||||
self.prev = prev
|
||||
|
||||
def append(self, data):
|
||||
if self.next == None:
|
||||
self.next = List(data, None, self)
|
||||
return self.next
|
||||
else:
|
||||
return self.next.append(data)
|
||||
|
||||
# Build the list
|
||||
tail = head = List(10)
|
||||
for i in [ 20, 30, 40 ]:
|
||||
tail = tail.append(i)
|
||||
|
||||
# Traverse forwards
|
||||
node = head
|
||||
while node != None:
|
||||
print(node.data)
|
||||
node = node.next
|
||||
|
||||
# Traverse Backwards
|
||||
node = tail
|
||||
while node != None:
|
||||
print(node.data)
|
||||
node = node.prev
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
l = [ 10, 20, 30, 40 ]
|
||||
for i in l:
|
||||
print(i)
|
||||
for i in reversed(l): # reversed produces an iterator, so only O(1) memory is used
|
||||
print(i)
|
||||
Loading…
Add table
Add a link
Reference in a new issue