Python nodes in a list
wordpress meta
title: 'Python Nodes In A List'
date: '2012-11-06T20:26:17-06:00'
status: publish
permalink: /python-nodes-in-a-list
author: admin
excerpt: ''
type: post
id: 68
category:
- Python
tag: []
post_format: []
title: 'Python Nodes In A List'
date: '2012-11-06T20:26:17-06:00'
status: publish
permalink: /python-nodes-in-a-list
author: admin
excerpt: ''
type: post
id: 68
category:
- Python
tag: []
post_format: []
Quick and simple Python list with node insert capabilty.
class Node:
def __init__(self,value):
self.data = value
self.next = 0
class List:
def __init__(self):
self.firstNode = Node(0)
def __ShowNodeData(self,aNode):
if aNode.next != 0:
print aNode.data
self.__ShowNodeData(aNode.next)
def Dump(self):
self.__ShowNodeData(self.firstNode)
def InsertAfter(self,aNode,aNewNode):
aNewNode.next = aNode.next
aNode.next = aNewNode
def InsertBeginning(self,aNewNode):
aNewNode.next = self.firstNode
self.firstNode = aNewNode
nodeA = Node("A")
nodeB = Node("B")
nodeC = Node("C")
nodeD = Node("D")
aList = List()
aList.InsertBeginning(nodeB)
aList.InsertAfter(nodeB,nodeD)
aList.InsertAfter(nodeD,nodeC)
aList.InsertAfter(nodeC,nodeA)
aList.Dump()