代码很乱 凑合看=。=
"""
# 11.33
class Node(object):
def __init__(self, value = None, next = None):
self.value = value
self.next = next
class LinkedList(object):
def __init__(self):
self.head = None
self.size = 0
def __repr__(self):
result = []
tmp = self.head
while tmp is not None:
result.append(tmp.value)
tmp = tmp.next
return str(result)
def append(self, value):
node = Node(value)
if self.head is None:
self.head = node
return True
tmp = self.head
while tmp.next is not None:
tmp = tmp.next
tmp.next = node
def remove(self, value):
tmp = self.head
if tmp.value == value:
self.head = None
return True
last = tmp
tmp = tmp.next
while tmp is not None:
if tmp.value == value:
last.next = tmp.next
return True
else:
tmp = tmp.next
last = last.next
print 'Not Found!'
return False
def find(self, value):
index = 0
tmp = self.head
while tmp is not None:
if tmp.value == value:
print 'Find at index: ', index
return index
else:
tmp = tmp.next
index += 1
print 'Not Found!'
return False
def push(self, value):
if self.head is None:
return False
node = Node(value)
node.next = self.head
self.head = node
def pop(self):
if self.head is None:
return False
value = self.head.value
self.head = self.head.next
return value
if __name__ == '__main__':
l = LinkedList()
l.append(1)
l.append(2)
l.append(3)
l.append(4)
l.append(6)
print l
l.remove(3)
print l
l.find(2)
l.push(10)
l.push(11)
print l
print l.pop()
print l.pop()
print l.pop()
"""