#! /usr/bin/env python
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
def list_add(pnode, data):
"""
@param pnode: the previous node
@param data: what to set the new nodes data to
"""
new_node = node()
new_node.data = data
new_node.next = pnode
return new_node
def list_print(nodelist):
print nodelist.data
if nodelist.next is not None:
list_print(nodelist.next)
a = node()
a.data = 1
b = list_add(a, 2)
c = list_add(b, 3)
list_print(c) # prints 3 2 1
Refactorings
No refactoring yet !
Simon Hartley
October 26, 2008, October 26, 2008 09:11, permalink
I think the benefit of the linked-list object would be that it can keep track of the head and tail nodes.
This means that as it knows the tail, the caller doesn't need to know the previous object in the list.
Benefits from the list knowing the head include: if you want to make the list iterable, then it always knows how to do this. You can also remove the head object easily from the list, useful if you are using the linked list in a FIFO Queue like way.
Nick Stinemates
November 2, 2008, November 02, 2008 18:03, permalink
Refactored to be more Object Oriented
#! /usr/bin/env python
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self,node=None):
node = ll.cur_node
while True:
try:
print node.data
node = node.next
except:
return
ll = linked_list()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)
ll.list_print()
Nick Stinemates
November 2, 2008, November 02, 2008 18:05, permalink
Refactored list_print()
#! /usr/bin/env python
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self):
node = ll.cur_node
while True:
print node.data
if node.next is not None:
node = node.next
else:
return
ll = linked_list()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)
ll.list_print()
BoySka
November 6, 2008, November 06, 2008 11:09, permalink
Refactored list_print (not tested, but should work)
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self):
node = ll.cur_node
while node:
print node.data
node = node.next
무파ì´
November 12, 2008, November 12, 2008 09:19, permalink
added 'pop_node' and rename 'add_node' to 'push_node'
class linked_list:
def __init__(self):
self.cur_node = None
def push_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def pop_node(self):
n = self.cur_node
if n:
self.cur_node = n.next
return n
def list_print(self):
node = self.cur_node
while node:
print node.data
node = node.next
satoru_Logic
March 7, 2009, March 07, 2009 07:46, permalink
The LinkedList class is changed to be an iterator.
#! /usr/bin/env python
class Node(object):
def __init__(self, data, next=None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def append(self, data):
new_node = Node(data)
if self.tail:
self.tail.next = new_node
else:
# If tail is None, we are adding to an empty list
self.head = new_node
self.tail = new_node
self.size += 1
def __iter__(self):
node = self.head
while node:
yield node
node = node.next
else:
raise StopIteration
def __str__(self):
return ('[' +
','.join([str(node) for node in self]) +
']')
if __name__ == '__main__':
ll = LinkedList()
for i in range(3):
ll.append(i)
print ll
Sean B.
February 13, 2010, February 13, 2010 00:30, permalink
Modified __str__ implementation of linked list iterator
def __str__(self):
return '[{0}]'.format(', '.join( [str(node) for node in self] ))
clonecd480
May 9, 2011, May 09, 2011 00:30, permalink
If you don't like the weather in New England, just wait a few minutes.
How can it be improved? I don't really like the way the list is constructed. Would it be more beneficial to encapsulate it in a linked_list object?