2015-05-13 18:35:05 +08:00
|
|
|
class Node(object):
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2016-06-13 11:29:58 +08:00
|
|
|
def __init__(self, data, next=None):
|
|
|
|
self.next = next
|
2015-05-13 18:35:05 +08:00
|
|
|
self.data = data
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-05-13 18:35:05 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.data
|
|
|
|
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-05-13 18:35:05 +08:00
|
|
|
class LinkedList(object):
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-05-14 19:30:13 +08:00
|
|
|
def __init__(self, head=None):
|
2015-05-13 18:35:05 +08:00
|
|
|
self.head = head
|
2015-05-14 19:34:35 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
curr = self.head
|
|
|
|
counter = 0
|
|
|
|
while curr is not None:
|
|
|
|
counter += 1
|
|
|
|
curr = curr.next
|
|
|
|
return counter
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-05-13 18:35:05 +08:00
|
|
|
def insert_to_front(self, data):
|
|
|
|
if data is None:
|
2016-06-13 11:29:58 +08:00
|
|
|
return None
|
|
|
|
node = Node(data, self.head)
|
|
|
|
self.head = node
|
2015-06-26 17:09:27 +08:00
|
|
|
return node
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2016-06-13 11:29:58 +08:00
|
|
|
def append(self, data):
|
2015-05-13 18:35:05 +08:00
|
|
|
if data is None:
|
2016-06-13 11:29:58 +08:00
|
|
|
return None
|
|
|
|
node = Node(data)
|
2015-05-13 18:35:05 +08:00
|
|
|
if self.head is None:
|
2016-06-13 11:29:58 +08:00
|
|
|
self.head = Node(data)
|
|
|
|
return node
|
|
|
|
curr_node = self.head
|
|
|
|
while curr_node.next is not None:
|
|
|
|
curr_node = curr_node.next
|
|
|
|
curr_node.next = node
|
2015-06-26 17:09:27 +08:00
|
|
|
return node
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-05-13 18:35:05 +08:00
|
|
|
def find(self, data):
|
|
|
|
if data is None:
|
2016-06-13 11:29:58 +08:00
|
|
|
return None
|
2015-05-13 18:35:05 +08:00
|
|
|
curr_node = self.head
|
|
|
|
while curr_node is not None:
|
|
|
|
if curr_node.data == data:
|
|
|
|
return curr_node
|
2016-06-13 11:29:58 +08:00
|
|
|
curr_node = curr_node.next
|
|
|
|
return None
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-05-13 18:35:05 +08:00
|
|
|
def delete(self, data):
|
|
|
|
if data is None:
|
|
|
|
return
|
|
|
|
if self.head is None:
|
|
|
|
return
|
2016-11-27 21:43:48 +08:00
|
|
|
if self.head.data == data:
|
|
|
|
self.head = None
|
|
|
|
return
|
2015-05-13 18:35:05 +08:00
|
|
|
prev_node = self.head
|
2016-11-27 21:43:48 +08:00
|
|
|
curr_node = self.head.next
|
2015-05-13 18:35:05 +08:00
|
|
|
while curr_node is not None:
|
|
|
|
if curr_node.data == data:
|
|
|
|
prev_node.next = curr_node.next
|
|
|
|
return
|
2016-11-27 21:43:48 +08:00
|
|
|
prev_node = curr_node
|
|
|
|
curr_node = curr_node.next
|
2015-05-13 18:35:05 +08:00
|
|
|
|
2016-06-13 11:29:58 +08:00
|
|
|
def delete_alt(self, data):
|
|
|
|
if data is None:
|
|
|
|
return
|
|
|
|
if self.head is None:
|
|
|
|
return
|
|
|
|
curr_node = self.head
|
|
|
|
if curr_node.data == data:
|
|
|
|
curr_node = curr_node.next
|
|
|
|
return
|
|
|
|
while curr_node.next is not None:
|
|
|
|
if curr_node.next.data == data:
|
|
|
|
curr_node.next = curr_node.next.next
|
|
|
|
return
|
|
|
|
curr_node = curr_node.next
|
|
|
|
|
2015-05-13 18:35:05 +08:00
|
|
|
def print_list(self):
|
|
|
|
curr_node = self.head
|
|
|
|
while curr_node is not None:
|
|
|
|
print(curr_node.data)
|
2015-06-26 17:09:27 +08:00
|
|
|
curr_node = curr_node.next
|
|
|
|
|
|
|
|
def get_all_data(self):
|
|
|
|
data = []
|
|
|
|
curr_node = self.head
|
|
|
|
while curr_node is not None:
|
|
|
|
data.append(curr_node.data)
|
|
|
|
curr_node = curr_node.next
|
|
|
|
return data
|