55502f40dc8b7c769880b10874abc9d0

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?

#! /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 !

81a73740df646bc6cccbfef6fab61ec9

Simon Hartley

October 26, 2008, October 26, 2008 09:11, permalink

No rating. Login to rate!

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.

44522fc90c3828034bf947f635fff4ce

Nick Stinemates

November 2, 2008, November 02, 2008 18:03, permalink

No rating. Login to rate!

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()
44522fc90c3828034bf947f635fff4ce

Nick Stinemates

November 2, 2008, November 02, 2008 18:05, permalink

No rating. Login to rate!

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()
2cf562e4076d37daa93b039fb7e666e5

BoySka

November 6, 2008, November 06, 2008 11:09, permalink

No rating. Login to rate!

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
55502f40dc8b7c769880b10874abc9d0

keeb

November 6, 2008, November 06, 2008 14:43, permalink

No rating. Login to rate!

That does look much cleaner.

5ddf71d9cb77ab778ac75f6d9864ee74

무파이

November 12, 2008, November 12, 2008 09:19, permalink

No rating. Login to rate!

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
4a3cb531d8b843df40093d82c0af79d0

satoru_Logic

March 7, 2009, March 07, 2009 07:46, permalink

No rating. Login to rate!

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
98f46668d0ec395baf6f848bf8509cec

Sean B.

February 13, 2010, February 13, 2010 00:30, permalink

No rating. Login to rate!

Modified __str__ implementation of linked list iterator

def __str__(self):
		return '[{0}]'.format(', '.join( [str(node) for node in self] ))
63f3ea721f9549c6ac448a1b60dfaddd

clonecd480

May 9, 2011, May 09, 2011 00:30, permalink

No rating. Login to rate!

If you don't like the weather in New England, just wait a few minutes.

Your refactoring





Format Copy from initial code

or Cancel